LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 09-17-2008, 01:30 AM   #1
raghu123
Member
 
Registered: May 2008
Posts: 34

Rep: Reputation: 15
selecting only the required portion of string in a line


Hi all,
I have n files like this.....

rot-matrix-3bwr_c.pdb-4hmg_c.pdb.txt
rot-matrix-3ckz_a.pdb-5hmg_a.pdb.txt
rot-matrix-5hmg_c.pdb-5hmg_e.pdb.txt

I want a command for getting only 4hmg_c.pdb ; 5hmg_a.pdb ; 5hmg_e.pdb
(i.e the letters after the third "-" but before ".txt")
SO that i can put the code in loop for n entries......for sample I have just pasted 3 entries......

Plz help
 
Old 09-17-2008, 01:48 AM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5
Posts: 16,086

Rep: Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995
Code:
echo rot-matrix-3bwr_c.pdb-4hmg_c.pdb.txt|cut -d'-' -f4|cut -d'.' -f1,2

4hmg_c.pdb
 
Old 09-17-2008, 01:51 AM   #3
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
You could use sed and create a regular expression that matches the part you want to save.
sed -s 's/rot-matrix-...._.\.pdb-\(.*\).txt/\1/' testfile
4hmg_c.pdb
5hmg_a.pdb
5hmg_e.pdb
 
Old 09-17-2008, 02:11 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 239Reputation: 239Reputation: 239
Code:
ls -1 rot* |while read line ; do a=${line##*-}; echo  ${a%.*}; done
 
Old 09-17-2008, 02:19 AM   #5
raghu123
Member
 
Registered: May 2008
Posts: 34

Original Poster
Rep: Reputation: 15
Smile

Its working fine..........Thanq
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 08:22 AM
shell script - matching portion of the line alenD Programming 11 10-02-2007 10:22 PM
How to identify a line and replace another string on that line using Shell script? Sid2007 Programming 10 10-01-2007 08:49 PM
selecting only letters of a string mohtasham1983 Programming 3 06-22-2005 03:48 PM


All times are GMT -5. The time now is 08:12 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration