Print all between 2 strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print all between 2 strings
# 1  
Old 01-08-2009
Print all between 2 strings

Hi All,

I'm working on a large file and need to extract all data between 2 strings. I have seen many good solutions to threads almost like my problem but none that quite fit.
This is all very new to me so any ideas would be really appreciated! (attempted to read sed and awk tutorials but got a headache trying to take it all in!)

Sample of data...

DEF on
DEF off
ABC on
DEF on
DEF off
GHI on
DEF on
ABC off
ABC on
ABC off
DEF off
etc..etc

There are duplications of the main strings I need to include in the output file, so I need to capture the first instance 'ABC on' and the last instance of 'ABC off'.
I need to extract all the data to resemble the output below..

ABC on (first occurence)
DEF on
DEF off
GHI on
DEF on
ABC off
ABC on
ABC off (last occurence)

Thanks In Advance.
Soots
# 2  
Old 01-08-2009
One way to give you the desired output:

Code:
awk 'NR==FNR {if($0=="ABC off"){r=NR};next}
/ABC on/{f=1}
FNR==r{print;exit}f' file file

Regards
# 3  
Old 01-08-2009
Thanks for the response...for some reason I get the following error messages:

awk: syntax error near line1
awk: bailing out near line 1

I've check the syntax of your script on my machine, it is as per your posting. Any thoughts appreciated.

Regards

Soots
# 4  
Old 01-08-2009
Quote:
Originally Posted by soots
Thanks for the response...for some reason I get the following error messages:

awk: syntax error near line1
awk: bailing out near line 1

I've check the syntax of your script on my machine, it is as per your posting. Any thoughts appreciated.

Regards

Soots
Use nawk or /usr/xpg4/bin/awk on Solaris.

Regards
# 5  
Old 01-08-2009
Did as suggested, used /usr/xpg4/bin/awk. Error messages went away but output did not materialise. The strings I'm using as begin and end placeholders are within a .dat file called machinelog.dat that looks similar to this

6/1/09 23:22:00 Machine1 Initialising : Initialising
6/1/09 23:22:01 Machine1 DEF on : system on
6/1/09 23:22:04 Machine1 ABC on : system on
6/1/09 23:22:05 Machine1 DEF on : system on
6/1/09 23:22:06 Machine1 ABC off : system off
6/1/09 23:22:07 Machine1 DEF off : system off
6/1/09 23:22:22 Machine1 ABC on : system on
6/1/09 23:22:25 Machine1 ABC off : system off
6/1/09 23:22:38 Machine1 ABC on : system on
6/1/09 23:23:07 Machine1 DEF on : system on
6/1/09 23:23:09 Machine1 ABC on : system on
6/1/09 23:23:15 Machine1 DEF on : system on
6/1/09 23:23:16 Machine1 ABC off : system failure
6/1/09 23:23:18 Machine1 DEF off : system off
6/1/09 23:23:22 Machine1 ABC on : system on
6/1/09 23:23:25 Machine1 ABC off : system failure
6/1/09 23:23:38 Machine1 ABC on : system on
6/1/09 23:24:07 Machine1 DEF on : system on

and I was hoping to capture everything between the first ABC on and the last system failure and put it into a new file called machinelogmain.dat, like below:

6/1/09 23:22:04 Machine1 ABC on : system on
6/1/09 23:22:05 Machine1 DEF on : system on
6/1/09 23:22:06 Machine1 ABC off : system off
6/1/09 23:22:07 Machine1 DEF off : system off
6/1/09 23:23:22 Machine1 ABC on : system on
6/1/09 23:23:25 Machine1 ABC off : system off
6/1/09 23:23:38 Machine1 ABC on : system on
6/1/09 23:22:07 Machine1 DEF on : system on
6/1/09 23:22:00 Machine1 ABC on : system on
6/1/09 23:22:01 Machine1 DEF on : system on
6/1/09 23:22:04 Machine1 ABC off : system failure
6/1/09 23:22:07 Machine1 DEF off : system off
6/1/09 23:23:22 Machine1 ABC on : system on
6/1/09 23:23:25 Machine1 ABC off : system failure

I tried to substitute the ABC on/ABC off etc but not much joy! Does your script format still apply or will it require a change?

Thanks for your patience.

Regards soots
# 6  
Old 01-09-2009
Try this

Code:
awk 'NR==FNR {if($0 ~ "system failure"){r=NR};next}
/ABC on/{f=1}
FNR==r{print;exit}f' file file

Regards
# 7  
Old 01-09-2009
Hi, I applied your solution this morning with not much joy. An output file was created but with no data inside? Unfortunetly I can't post the actual data for you to try. I'll keep plugging away at it.Thanks for your help with this problem.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print text between 2 identical strings

hey, i m having a hard time trying to print only the first occurrence between 2 idenicale strings. for the following output: please help me im a noob please im a noob help me noob please help me im a noob please im a noob help me noob (3 Replies)
Discussion started by: boaz733
3 Replies

2. Shell Programming and Scripting

Print lines between strings like *0123456*

I have a text file contains *02638650* SAMBO 1 Spouse SAMBO FELIX *01591453* MADUAGUGBUO 4 Child3 MADUAGUGBUO JOY *01488523* ANYIAM 1 Spouse ANYIAM FRANCA 2 Child1 ANYIAM GRACE *01647769* EGWUTUOHA 0 Principal ... (6 Replies)
Discussion started by: ktsis
6 Replies

3. Programming

Print only some strings from an output

Hi, Here is an example: I have a grep line: grep -i -r -H "$WORD" "$DIRECTORY"with an output like this: /media/dir/dir2//dir4/file.txt:/media/dir/dir2/dir3/file_16072008/es6.txt: "content of the file found from grep"/media/dir/dir2/dir3/dir4/file3.txt:/media/dir/dir2/dir3//file.txt:"other... (3 Replies)
Discussion started by: Hornys
3 Replies

4. Shell Programming and Scripting

How to print few strings in a line?

I have one script which gave final output as the below statment. Successfully added Nomination Petition Manifest to the Content Manager. Now i want a sed statement which will search for this statement in any given file and print only Nomination Petition Manifest. I don't want to hardcore... (13 Replies)
Discussion started by: Vikram_Tanwar12
13 Replies

5. Shell Programming and Scripting

[Help me!] Print text between two strings

Deal All, I have problem for this: input file : "data.txt" R 240 585694.59946146.8 8.0 239 585694.09946134.3 8.0 238 585693.59946121.8 8.01R 237 585693.09946109.3 8.0 236 585692.59946096.9 8.0 235 585692.19946084.4 8.01R 234 585691.59946071.9 8.0 233 585691.09946059.5 8.0 232... (2 Replies)
Discussion started by: aksin
2 Replies

6. Shell Programming and Scripting

[Help me!] print text between two strings

Deal All, I have problem for this: input file : "data.txt" R 240 585694.59946146.8 8.0 239 585694.09946134.3 8.0 238 585693.59946121.8 8.01R 237 585693.09946109.3 8.0 236 585692.59946096.9 8.0 235 585692.19946084.4 8.01R 234 585691.59946071.9 8.0 233 585691.09946059.5 8.0 232... (2 Replies)
Discussion started by: aksin
2 Replies

7. UNIX for Dummies Questions & Answers

Get strings on a file and print

hi all! i have a file like this lea 25 female dave 18 male jake 27 male and i want to have an output file like this my name is lea. i am 25. female my name is dave. i am 18. male my name is jake. i am 27. male thanks! (2 Replies)
Discussion started by: engr.jay
2 Replies

8. Shell Programming and Scripting

print specific strings only

Hello, I have a file like this.. 2 168611167 STK39 STK39 --- 27347 "serine threonine kinase 39 (STE20/SPS1 homolog, yeast)" YES SNP_A-2086192 rs16854601 0.001558882 6 13670256 SIRT5 /// RPS4X SIRT5 --- 23408 /// 6191 "sirtuin (silent mating type... (5 Replies)
Discussion started by: genehunter
5 Replies

9. Shell Programming and Scripting

Print all the lines between 2 specified strings

Hi All, I have a file in which i want to print all the lines between 2 defined strings. Ex- I have file with data as follows STEP1:- ----- has some 20 -30 lines of data STEP2:- ----- has some 20 -30 lines of data So i want to print those lines between STEP1 & STEP2. (line including STEP1)... (7 Replies)
Discussion started by: digitalrg
7 Replies

10. Shell Programming and Scripting

How to print only lines in between two strings using awk

Hi, I want to print only lines in between two strings and not the strings using awk. Eg: OUTPUT top 2 bottom 1 left 0 right 0 page 66 END I want to print into a new file only top 2 bottom 1 left 0... (4 Replies)
Discussion started by: jisha
4 Replies
Login or Register to Ask a Question