Regarding multiline record searching with specific pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Regarding multiline record searching with specific pattern
# 1  
Old 05-08-2011
Regarding multiline record searching with specific pattern

Dear Experts,

I need to extract specific records from one file which has multiline records.

Input file pattern is:

============
aaaaaaaa
bbbbbbbb
asdf 1234
cccccccc
dddddddd

============
aaaaaaaa
bbbbbbbb
qwer 2345
cccccccc
dddddddd

============
qqqqqqqq
rrrrrrrrrrrrr
asdf 1234
cccccccc
dddddddd

============

In output I only want records which have "asdf 1234" as pattern:

============
aaaaaaaa
bbbbbbbb
asdf 1234
cccccccc
dddddddd

============
qqqqqqqq
rrrrrrrrrrrrr
asdf 1234
cccccccc
dddddddd

============

Pls suggest how this can be done.

Thanks,
Dhiraj
# 2  
Old 05-08-2011
Do you want 2 lines above and below the pattern?
Requirement is not very clear.

regards,
Ahamed
# 3  
Old 05-08-2011
Hi,

I want entire record which is delimited by "===========" pattern.
Record can be of variable size (i.e lines count can change).
But it will start & end with "=============".

============
aaaaaaaa
bbbbbbbb
asdf 1234
cccccccc
dddddddd
asdsdsaff

============




Thanks,
Dhiraj
# 4  
Old 05-08-2011
Try Smilie
Code:
cat input | tr "\n" " " | tr "=" "\n" | awk '/asdf 1234/ { printf $0 } ' | tr " " "\n"

# 5  
Old 05-08-2011
Try this

Code:
awk '/^=/{if(s==1){s=0}else{s=1}}{x[++i]=$0}/asdf 1234/{f=1};{if(f==1&&s==0){for(j=0;j<i;j++){if(x[j]){print x[j]}}i=0;}}' file

regards,
Ahamed
# 6  
Old 05-08-2011
Sorry for my post above, i belive this code should work much better

Code:
awk '{ RS="============" } /asdf 1234/ { print $0  } ' input  ## or printf $0RS"\n"

# 7  
Old 05-08-2011
Quote:
Originally Posted by Peasant
Sorry for my post above, i belive this code should work much better

Code:
awk '{ RS="============" } /asdf 1234/ { print $0  } ' input  ## or printf $0RS"\n"

Nice one!
I though "=====" may not be constant. Smilie

regards,
Ahamed
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

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

4. Red Hat

Moving of file content to another two files after searching with specific pattern

Hello, Please help me with this!! Thanks in advance!! I have a file named file.gc with the content: 1-- Mon Sep 10 08:53:09 CDT 2012 2revoke connect from FR2261; 3delete from mkt_allow where grantee = 'FR2261'; 4grant connect to FR2261 with '******'; 5alter user FR2261 comment... (0 Replies)
Discussion started by: raosr020
0 Replies

5. Shell Programming and Scripting

Print first and last line from multiline record

Hi - I'm new to working with multiline records and I'm going nuts trying to do something that seems simple. Input: Tue May 1 14:00 Header Record 1 is valid. Tue May 1 14:00 processing data to 25-Mar-2012 09:00:23.15 Tue May 1 14:03 Header Record 1 is valid. Tue May 1 14:03 processing data... (4 Replies)
Discussion started by: Catullus
4 Replies

6. Shell Programming and Scripting

Reject the record if the record in the next line does not satisfy the pattern

Hi, I have a input file with the following entries: 1one 2two 3three 1four 2five 3six 1seven 1eight 1nine 2ten The output should be 1one 2two 3three 1four 2five 3six (2 Replies)
Discussion started by: supchand
2 Replies

7. Shell Programming and Scripting

multiline pattern matching

Hi, I have a file of the following from: Afghanistan gdpcapit|800 Akrotiri Albania gdpcapit|6000 now I want have the gdpcapit value next to the country when there is one like this: Afghanistan 800 gdpcapit|800 Akrotiri Albania 6000 gdpcapit|6000 How do I do this? I've... (4 Replies)
Discussion started by: KarelVH
4 Replies

8. Shell Programming and Scripting

Multiline read with multicharacter record seperator

I have a file like the below: Start <</NumCopies 0001>> 0223 098 et(5926)sh 0223 098 mt(5926)sh End Start <</NumCopies 0001>> 0224 098 et(5926)sh 0224 098 mt(5926)sh End This file needs to be split to seperate files. Each of the seperate file will need to... (3 Replies)
Discussion started by: pt14
3 Replies

9. Shell Programming and Scripting

Awk Multiline Record Combine?

I'm trying to use Awk to get the id and name fields ($1 and $2) of file1 combined with their corresponding multiline records in file2 that are separated by blank line. Both files are ordered so that the first line of file1 corresponds to the first set of multiline records in file2 and so on. ... (4 Replies)
Discussion started by: RacerX
4 Replies

10. Shell Programming and Scripting

transforming a multiline record to single line

Hi All I have a file like this <LText>gvsvdkag<LREC>bdjvdj</LREC>nididyvv</LText> <LText>gvsvdkag<LREC>bdj vdj</LREC>nididyvv</LText> <LText>gvsvdkag<LREC>b djvdj</LREC>nididyvv</LText> <LText>gvsvdkag<LREC>bdjvdj</LREC>nididyvv</LText> How will i change the file to ... (9 Replies)
Discussion started by: anju
9 Replies
Login or Register to Ask a Question