LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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, 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
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,360

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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



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 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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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
Open Source Consulting | Domain Registration