Find a string and print all lines upto another string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find a string and print all lines upto another string
# 1  
Old 03-11-2015
Find a string and print all lines upto another string

Ok I would like to do the following

file test contains the following lines. between the lines ABC there may be any amount of lines up to the next ABC entry.
I want to grep for the filename.txt entry and print the lines in between (and including that line) up to and including the last line before the next ABC
I want do this so I could also specify filename3.txt and get all 4 lines in between.
All help much appreciated.

Code:
ABC  4 5 filename.txt 3
line 1 2 3 4 5 
line 1 2 3 4 5
ABC 4 5 filename2.txt 5
line 1 2 3 4 6 
ABC 6 6 filename3.txt 6
line 1 2 3 4 5 
line 3 4 5 6 7
line 5 6 6 7 8
line 4 3 2 1 3
ABC 4 3 filename44.txt 5

any ideas?

---------- Post updated at 04:15 PM ---------- Previous update was at 03:40 PM ----------

Think I've got it
Code:
 awk '/string_to_search/ {a=1;next} /ABC/{f=0} f {print}'


Last edited by rbatte1; 03-11-2015 at 03:01 PM.. Reason: Added CODE tags and corrected spelling and case
# 2  
Old 03-11-2015
With the correct order you can save a next:
Code:
awk '/ABC/ {f=0} ($0 ~ search) {f=1} f' search="filename3"

or
Code:
awk '/ABC/ {f=0} f; ($0 ~ search) {f=1}' search="filename3"

# 3  
Old 03-12-2015
MadeInGermany,

I cannot get your script to work.

I am having to loop what I have in my filename.txt search

so I am doing a

Code:
for file in `cat filename`
do
awk '/ABC/ {f=0} ($0 ~ search) {f=1} f' search="$file" /anotherfile,txt
done


this fails with
Code:
awk: /ABC/ {f=1} (./fragfind ~ search (f=1) f
awk:                ^ syntax error


Last edited by Don Cragun; 03-12-2015 at 03:36 PM.. Reason: Add CODE tags.
# 4  
Old 03-12-2015
Please use code tags as required by forum rules!

I'm pretty sure
Code:
awk '/ABC/ {f=0} ($0 ~ search) {f=1} f' search="$file" /anotherfile,txt

will give the error message
Code:
awk: /ABC/ {f=1} (./fragfind ~ search (f=1) f
awk: ^ syntax error

Are you sure you have the quoting correct? And, the comma in the filename? And, why do you assign 1 to f twice?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print all lines after a string

My file has the following contents... User:SYS,O/SUser:oracle,Process:10813484,Machine:host123.xxx.xxx.xxxxxxx.com.au,Program:sqlplus@host123.xxx.xxx.xxxxxxx.com.au(TNSV1-V,LogonTime:24-JUL-2014 15:04 I would like to print all character appearing after the string LogonTime: My output... (5 Replies)
Discussion started by: Nagesh_1985
5 Replies

2. Shell Programming and Scripting

Grep for the string and then print the next 4 lines

RHEL 5.8 I have a text file like below. I want to grep for a string and then print the next 4 lines including the line with the string I grepped for For eg: I want grep for the string HANS and then print the next 4 lines including HANS $ cat someText.txt JOHN NATIONALITY:... (7 Replies)
Discussion started by: omega3
7 Replies

3. Shell Programming and Scripting

How to print the lines in a file for the given string?

Hi, I have a file with contents test id text day test sah dh dhs yeay fg jsh jsjk my need: I give a string as a input, it check the file and display the lines with the given string e.g input : test output: test id text day test sah dh dhs (1 Reply)
Discussion started by: nanthagopal
1 Replies

4. UNIX for Dummies Questions & Answers

find string and and print another string

i have a file that looks like this ABC123 aaaaaaaaaaaaaaasssssssssssssssffhhh ABC234 EMPTY ABC652 jhfffffffffffffffffffffffffffffffffffkkkkkkkkkkkk i want to grep "EMPTY" and print ABC234 (3 Replies)
Discussion started by: engr.jay
3 Replies

5. Shell Programming and Scripting

Awk - find string, search lines below string for other strings

What's the easiest way to search a file for a specific string and then look for other instances after that? I want to search for all Virtual Hosts and print out the Server Name and Document Root (if it has that info), while discarding the rest of the info. Basically my file looks like this: ...... (6 Replies)
Discussion started by: Mbohmer
6 Replies

6. Shell Programming and Scripting

awk find a string, print the line 2 lines below it

I am parsing a nagios config, searching for a string, and then printing the line 2 lines later (the "members" string). Here's the data: define hostgroup{ hostgroup_name chat-dev alias chat-dev members thisisahostname } define hostgroup{ ... (1 Reply)
Discussion started by: mglenney
1 Replies

7. Shell Programming and Scripting

Print lines between two lines after grep for a text string

I have several very large file that are extracts from Oracle tables. These files are formatted in XML type syntax with multiple entries like: <ROW> some information more information </ROW> I want to grep for some words, then print all lines between <ROW> AND </ROW>. Can this be done with AWK?... (7 Replies)
Discussion started by: jbruce
7 Replies

8. Shell Programming and Scripting

Find a string in textfile, erase $num lines after that string

I have a textfile containing text similar to the following pattern: STRING1 UNIQUE_STRING1 STRING2 STRING3 STRING4 STRING5 STRING1 UNIQUE_STRING2 STRING2 STRING3 STRING4 STRING5 STRING1 UNIQUE_STRING3 STRING2 STRING3 (6 Replies)
Discussion started by: ilcsfe
6 Replies

9. Shell Programming and Scripting

Need help to print lines contains particular string format in a file

Hi, I want to print the lines in a file that matches particular string format using shell scripting. (4 Replies)
Discussion started by: sudhakaryadav
4 Replies

10. Shell Programming and Scripting

print only matched string instead lines in grep

frnd, Is there any way to print only the string that matched the pattern instead printing the whole line? thanks in advance. (3 Replies)
Discussion started by: clx
3 Replies
Login or Register to Ask a Question