Grep for a pattern and print entire record


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep for a pattern and print entire record
# 1  
Old 07-09-2009
Grep for a pattern and print entire record

Hi friends,
This is my very first post on forum, so kindly excuse if my doubts are found too silly.
I am trying to automate a piece of routine work and this is where I am stuck at the moment-I need to grep a particular ID through a file containing many records(which start with <LRECORD> and end with </LRECORD>), and print only those records which have the matching ID.
As an example, my input file contents are as below:

Code:
<LRECORD>alskdkfdsfk
fsdjklfnsdf
fsjdfndjn</LRECORD>
fdsfdfg
<LRECORD>123
f42
5345</LRECORD>

Now supposing I need to print only the record containing f42, how do I do that? Smilie

Last edited by Yogesh Sawant; 07-10-2009 at 04:24 AM.. Reason: added code tags
# 2  
Old 07-10-2009
Java Grep for a pattern and print entire record

Dear Faiz,,


If the lines between the <LRECORD> </LRECORD> pair is unique you can use the -A and -B option of the grep command. For example in your post the lines b/w the<LRECORD> </LRECORD> pair is 1 so you can use the command:

grep 'f42' ./input.txt -A 2 -B 1

Hope this will help.
# 3  
Old 07-10-2009
If you want to find the record containing f42, you can do like this:
Code:
awk 'BEGIN{RS="(<LRECORD>|<\/LRECORD>)"; FS="";}/f42/ {print $RS} ' yourfile.txt

# 4  
Old 07-10-2009
Quote:
Originally Posted by thanhdat
If you want to find the record containing f42, you can do like this:
Code:
awk 'BEGIN{RS="(<LRECORD>|<\/LRECORD>)"; FS="";}/f42/ {print $RS} ' yourfile.txt


@thanhdat
This is not working for me.

I suggest this...
Code:
awk '/<LRECORD>/ {
   RS="</LRECORD>"
   FS="LRECORD>"
}
/f42/{ print $2
}' input

# 5  
Old 07-13-2009
My awk version is: mawk 1.3.3 (default on Debian Lenny). I also tested with gawk 3.1.5 and both your solution and mine work ^^
What's your awk version and what was the error message ? Maybe I can avoid it next time. Thankssss
# 6  
Old 07-13-2009
I dont know my version of awk...
The error was...
Code:
awk: Field (<LRECORD>|</LRECORD>) is not correct.
 The input line number is 1. The file is datafile.txt.
 The source line number is 1.

# 7  
Old 07-20-2009
@rakeshawasthi: maybe some awk versions don't support the syntax "\/LRECORD" °__° I'll try to use another way next time. Thanks for your report.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find key pattern and print selected lines for each record

Hi, I need help on a complicated file that I am working on. I wanted to extract important info from a very huge file. It is space delimited file. I have hundred thousands of records in this file. An example content of the inputfile as below:- ## ID Ser402 Old; 23... (2 Replies)
Discussion started by: redse171
2 Replies

2. AIX

Grep a pattern and print following n lines

Hi all, I am struck with the below requirement. I need to grep a particular pattern in a file and then print next n lines of it for further processing. I have used the below code grep -A 3 "pattern" filename But it is throwing error as below. grep: illegal option -- A Can... (14 Replies)
Discussion started by: ssk250
14 Replies

3. Shell Programming and Scripting

Help with print out record if first and next line follow specific pattern

Input file: pattern1 100 250 US pattern2 50 3050 UK pattern3 100 250 US pattern1 70 1050 UK pattern1 170 450 Mal pattern2 40 750 UK . . Desired Output file: pattern1 100 250 US pattern2 50 3050 UK pattern1 170 450 Mal pattern2... (3 Replies)
Discussion started by: cpp_beginner
3 Replies

4. Shell Programming and Scripting

Execution problem with print out record that follow specific pattern

Hi, Do anybody know how to print out only those record that column 1 is "a" , then followed by "b"? Input file : a comp92 2404242 2405172 b comp92 2405303 2406323 b comp92 2408786 2410278 a comp92 2410271 2410337 a comp87 1239833 1240418 b comp87... (3 Replies)
Discussion started by: patrick87
3 Replies

5. Shell Programming and Scripting

awk to print record not equal specific pattern

how to use "awk" to print any record has pattern not equal ? for example my file has 5 records & I need to get all lines which $1=10 or 20 , $2=10 or 20 and $3 greater than "130302" as it shown : 10 20 1303252348212B030 20 10 1303242348212B030 40 34 1303252348212B030 10 20 ... (14 Replies)
Discussion started by: arm
14 Replies

6. UNIX for Dummies Questions & Answers

Match pattern in a field, print pattern only instead of the entire field

Hi ! I have a tab-delimited file, file.tab: Column1 Column2 Column3 aaaaaaaaaa bbtomatoesbbbbbb cccccccccc ddddddddd eeeeappleseeeeeeeee ffffffffffffff ggggggggg hhhhhhtomatoeshhh iiiiiiiiiiiiiiii ... (18 Replies)
Discussion started by: lucasvs
18 Replies

7. Shell Programming and Scripting

Help with print out all relevant record if match particular pattern

Input file: data100_content1 420 700 data101_content1 107 516 data101_content2 194 773 data101_content3 195 917 data104_content2 36 325 data105_content1 505 605 data106_content1 291 565 ... (7 Replies)
Discussion started by: perl_beginner
7 Replies

8. Shell Programming and Scripting

Print the above and below lines for the grep pattern.

Hi, i would like to get the above and below lines of the grep pattern . For ex : file as below: chk1- aaaa 1-Nov chk2 -aaaa ########## chk1-bbbbbb 1-Nov chk2-bbbbbb ######### my search pattern is date : 1-Nov i need the o/p as below chk1- aaaa 1-Nov (6 Replies)
Discussion started by: expert
6 Replies

9. Solaris

Using grep to print just the pattern match

Hi all, Is it possible for grep to output just the pattern match and not the whole line when it comes across a match? I know you can adjust the number of trailing or leading lines that are printed, but am yet to find anything that outputs just the pattern match. Cheers, Tim (5 Replies)
Discussion started by: muzzaw
5 Replies

10. Shell Programming and Scripting

Awk+Grep Input file needs to match a column and print the entire line

I'm having problems since few days ago, and i'm not able to make it works with a simple awk+grep script (or other way to do this). For example, i have a input file1.txt: cat inputfile1.txt 218299910417 1172051195 1172070231 1172073514 1183135117 1183135118 1183135119 1281440202 ... (3 Replies)
Discussion started by: poliver
3 Replies
Login or Register to Ask a Question