LinuxQuestions.org
Register a domain and help support LQ
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
 
Search this Thread
Old 09-17-2008, 02: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, 02:48 AM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
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, 02:51 AM   #3
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
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, 03:11 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Code:
ls -1 rot* |while read line ; do a=${line##*-}; echo  ${a%.*}; done
 
Old 09-17-2008, 03: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


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 09:22 AM
shell script - matching portion of the line alenD Programming 11 10-02-2007 11:22 PM
How to identify a line and replace another string on that line using Shell script? Sid2007 Programming 10 10-01-2007 09:49 PM
selecting only letters of a string mohtasham1983 Programming 3 06-22-2005 04:48 PM


All times are GMT -5. The time now is 02:05 PM.

Main Menu
Advertisement
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