LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-06-2013, 07:47 PM   #1
SuzuBell
LQ Newbie
 
Registered: Dec 2013
Posts: 3

Rep: Reputation: Disabled
Separating a file by two tokens


Hello Linux Gurus:

I have an Open Office Document.

In this file, many times it has a "start token line" that starts with the word "MODEL", followed by a number of spaces, followed by a number (For example: MODEL 1, MODEL 2, MODEL 3, MODEL 4, MODEL 5, .....etc).

After each "start token line", there are many lines that end with an "end token line" that consists only of the word "ENDMDL".

I would like to parse the file so that it grabs all lines starting from (and including) the "start token line" and ending at (and including) the "end token line" into a new output file.

In other words, if I ran this on a file with 100 of these "start token line" and "end token line", I would like to produce 100 files.

Any suggestions would be appreciated! I have parsed a file, but not tried a double token approach before...
 
Old 12-06-2013, 08:22 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,056

Rep: Reputation: 1916Reputation: 1916Reputation: 1916Reputation: 1916Reputation: 1916Reputation: 1916Reputation: 1916Reputation: 1916Reputation: 1916Reputation: 1916Reputation: 1916
perl or awk sound best - set a flag when you find the MODEL line and set/increment a filecounter variable. Write to that filecounter until ENDMDL at which time you reset the flag. An awk one-liner should be pretty quick.
 
Old 12-07-2013, 01:00 PM   #3
SuzuBell
LQ Newbie
 
Registered: Dec 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thanks!

Thank you. I have it working with:

awk '/MODEL/ {flag=1;next} /ENDMDL/{flag=0} flag {print}' 1KZS.pdb > TEST

However, It is just printing all lines between MODEL-->ENDMDL into the *same* output file. But I would like to separate it so that each MODEL-->ENDMDL is in a new output file.

Is there a way my awk command can be tweaked to accomplish this?
 
Old 12-07-2013, 01:46 PM   #4
vl23
Member
 
Registered: Mar 2009
Posts: 125

Rep: Reputation: 8
counter=0
for line in `yourawkscripthere`;
do
echo $line >OUTPUTFILENASENAME$counter;
counter=((counter+1));
done

That should do what I believe you want to do.
 
Old 12-07-2013, 07:38 PM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,056

Rep: Reputation: 1916Reputation: 1916Reputation: 1916Reputation: 1916Reputation: 1916Reputation: 1916Reputation: 1916Reputation: 1916Reputation: 1916Reputation: 1916Reputation: 1916
I thought you wanted to include the MODEL/ENDMDL - easy enough done.

Similar to @vl23, you can direct the print in awk - have a look at this; note the use of a counter as I suggested above
Code:
awk '/MODEL/ {flag=1;cntr++;next} /ENDMDL/{flag=0} flag {print > "file"cntr}' 1KZS.pdb
 
  


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
Extract tokens from XML file hanae Programming 22 05-25-2012 07:12 PM
SSH authentication using existing AD tokens mp1smw Linux - Enterprise 2 11-02-2008 07:43 AM
bash - getting tokens from string klavuzkarga Linux - Newbie 6 12-14-2007 02:13 PM
Trouble printing tokens with C. smoothdogg00 Programming 21 02-06-2006 07:04 PM


All times are GMT -5. The time now is 06:25 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration