Print lines between two lines after grep for a text string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print lines between two lines after grep for a text string
# 1  
Old 09-28-2010
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?

thanks, jbruce
# 2  
Old 09-28-2010
I don't know how your file is formatted but you can give this a try:
Code:
awk '/<pattern>/{gsub("\n$|^\n","");print}' RS="<ROW>|</ROW>" file

# 3  
Old 09-28-2010
Hi, just a quick note about RS, this does not work in every awk, from the POSIX specification:
Quote:
The first character of the string value of RS shall be the input record separator; a <newline> by default. If RS contains more than one character, the results are unspecified.
awk:Variables and Special Variables
# 4  
Old 09-28-2010
@Scrutinizer, thanks for mention that Smilie, an adapted version:
Code:
awk '/<pattern>/{gsub("\n$|^\n|ROW>\n","");print}' RS="<" file

Best regards
# 5  
Old 09-28-2010
Print lines between two lines after grep for a text string

Franklin,
file format is
<ROW>
<OBJECT_ID>32948</OBJECT_ID>
<OBJECT_SEQ_NO>1</OBJECT_SEQ_NO>
<OBJECT_DATA> as much as 24 lines X 80 characters </OBJECT_DATA>
</ROW>

OR

<ROW>
<OBJECT_ID>32948</OBJECT_ID>
<OBJECT_SEQ_NO>2</OBJECT_SEQ_NO>
<OBJECT_DATA> or just a single line /OBJECT_DATA>
</ROW>
this could be repeated a thousand times, of course with different information between the <ROW> and </ROW>. I did try your suggestion and got this output:
awk: Line OBJECT_DATA>NAU-AKL- cannot have more than 199 fields.
The input line number is 33. The file is ORACLE_OBJECT_001.xml.good.
The source line number is 1.
# 6  
Old 09-29-2010
Try this approach:
Code:
awk '
/<\/ROW>/ && b {print s; exit}
/<ROW>/{s="";next}
/<pattern>/{b=1}
{s=s?s RS $0:$0}' file

# 7  
Old 09-29-2010
Print lines between two lines after grep for a text string

Franklin, Thank you for the time you have taken to answer my query. The latest code works and produces the output that I wanted, however it will stop at the first occurrence of the <pattern>. In the file I am using for my test there are 19 of the key words.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep three consecutive lines if each lines contains certain string

say we have : 2914 | REQUEST | whatever 2914 | RESPONSE | whatever 2914 | SUCCESS | whatever 2985 | RESPONSE | whatever 2986 | REQUEST | whatever 2990 | REQUEST | whatever 2985 | RESPONSE | whatever 2996 | REQUEST | whatever 2010 | SUCCESS | whatever 2013 | REQUEST | whatever 2013 |... (7 Replies)
Discussion started by: Saumitra Pandey
7 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

Grep couple of consecutive lines if each lines contains certain string

Hello, I want to extract from a file like : 20120530025502914 | REQUEST | whatever 20120530025502968 | RESPONSE | whatever 20120530025502985 | RESPONSE | whatever 20120530025502996 | REQUEST | whatever 20120530025503013 | REQUEST | whatever 20120530025503045 | RESPONSE | whatever I want... (14 Replies)
Discussion started by: black_fender
14 Replies

4. Shell Programming and Scripting

Print lines before and after..not grep -A

Hi I have this in my file 2011-04-18 15:32:11 system-alert-00012: UDP flood! From xxxxxx to yyyyyyyyyy, int ethernet0/2). Occurred 1 times. 2011-04-18 15:32:11 system-alert-00012: UDP flood! From xxxxxx to yyyyyyyyyy, int ethernet0/2). Occurred 1 times. 2011-04-18 15:32:11... (9 Replies)
Discussion started by: zorrox
9 Replies

5. Shell Programming and Scripting

print lines AFTER lines cointaining a regexp (or print every first and fourth line)

Hi all, This should be very easy but I can't figure it out... I have a file that looks like this: @SRR057408.1 FW8Y5CK02R652T length=34 AGCAGTGGTATCAACGCAGAGTAAGCAGTGGTAT +SRR057408.1 FW8Y5CK02R652T length=34 FIIHFF6666?=:88@@@BBD:::?@ABBAAA>8 @SRR057408.2 FW8Y5CK02TBMHV length=52... (1 Reply)
Discussion started by: kmkocot
1 Replies

6. Shell Programming and Scripting

print string at the end of lines in text file

hello, I go text file like this E:/DDD/Dyndede/wwww E:/DDD/sss.com/ffffg/fff E:/DDD/vvvvvv/dd E:/DDD/sss.com/bbbbbb E:/DDD/sss.com/nnnn/xxI want to print /alpha.jpg at the end of every lines like that E:/DDD/Dyndede/wwww/alpha.jpg E:/DDD/sss.com/ffffg/fff/alpha.jpg... (8 Replies)
Discussion started by: davidkhan
8 Replies

7. Shell Programming and Scripting

AIX equivalent to GNU grep's -B and -A [print lines after or before matching lines]

Hi folks I am not allowed to install GNU grep on AIX. Here my code excerpt: grep_fatal () { /usr/sfw/bin/gegrep -B4 -A2 "FATAL|QUEUE|SIGHUP" } Howto the same on AIX based machine? from manual GNU grep ‘--after-context=num’ Print num lines of trailing context after... (4 Replies)
Discussion started by: slashdotweenie
4 Replies

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

9. Shell Programming and Scripting

Print lines after grep

Hi all, I need help in following scenario. I have a file with about 10,000 lines. There are several lines which have word "START" (all upper case) in them. I want to grep line with word "START" and then do the following 1. Print the line number having word "START" 2. Print the next 11 lines. ... (4 Replies)
Discussion started by: jakSun8
4 Replies
Login or Register to Ask a Question