print records before and after the pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting print records before and after the pattern
# 1  
Old 06-02-2009
print records before and after the pattern

Hi All,

I want to write a sh shell script to read input report and search for a specefic pattern in file and print 3 record above and 2 record below the line in which pattern found.

My file contains different- different pattern repeatedly, same pattern set of records should go into one file and similiarly for other type of pattern

input file for example

Program
Date
TIME
Program ID: ABC
Message
End of Program

Program
Date
TIME
Program ID: PQR
Message
End of Program

Program
Date
TIME
Program ID: XYZ
Message
End of Program

Program
Date
TIME
Program ID: ABC
Message
End of Program

Program
Date
TIME
Program ID: XYZ
Message
End of Program


in this example , The scripts should create three files having records set for Program ID: XYZ in first file
Program ID: ABC in second file
Program ID: PQR in third file

Thanks in Advance

Singhal
# 2  
Old 06-02-2009
use this..
Code:
awk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' b=3 a=2 s="pattern" filename

# 3  
Old 06-02-2009
The solution is not working. the pattern is : Program ID based on i need to split the files which will contians record set for all smimilar Program ID values
# 4  
Old 06-02-2009
Quote:
Originally Posted by vidyadhar85
use this..
Code:
awk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' b=3 a=2 s="pattern" filename

vidyadhar85,

I don't get any output with your command.

singhald,

This should create the files as desired:

Code:
awk 'BEGIN{FS=OFS="\n"}{print $0 "\n" > $4}' RS= file

Regards
# 5  
Old 06-02-2009
Quote:
Originally Posted by Franklin52
vidyadhar85,

I don't get any output with your command.
Quote:
Originally Posted by singhald
Hi All,

I want to write a sh shell script to read input report and search for a specefic pattern in file and print 3 record above and 2 record below the line in which pattern found.
Code:
 
home/> cat vvv
2
34
42
1
45
1
12
home/l> awk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' b=3 a=2 s="45" vvv
34
42
1
45
1
12

# 6  
Old 06-02-2009
Quote:
Originally Posted by vidyadhar85
Code:
 
home/> cat vvv
2
34
42
1
45
1
12
home/l> awk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' b=3 a=2 s="45" vvv
34
42
1
45
1
12

Have you tried your solution with the given file of the OP?
The OP wants the output in 3 files, check the 1st post.

Regards
# 7  
Old 06-02-2009
Wow!!! Franklin your solution is working fine. Thanks you Franklin, vidyadhar for your valuable and fast response

Singhal

-----Post Update-----

Hi Franklin,

Now if input file dont have any blank line after the set of records, would this code work fro below ex.

Program ID: ABC
Message
End of Program
Program:2
Date:
TIME:
Program ID: PQR
Message
End of Program
Program:3
Date:
TIME:
Program ID: XYZ
Message
End of Program
Program:4
Date:
TIME:
Program ID: ABC
Message
End of Program
Program:5
Date:
TIME:
Program ID: XYZ
Message
End of Program

Please suggest me .....

Last edited by singhald; 06-02-2009 at 08:54 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

2. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

3. UNIX for Dummies Questions & Answers

Match Pattern after certain pattern and Print words next to Pattern

Hi experts , im new to Unix,AWK ,and im just not able to get this right. I need to match for some patterns if it matches I need to print the next few words to it.. I have only three such conditions to match… But I need to print only those words that comes after satisfying the first condition..... (2 Replies)
Discussion started by: 100bees
2 Replies

4. Shell Programming and Scripting

Searching for a pattern and extracting records related to that pattern

Hi there, Looking forward to your advice for the below: I have a file which contains 2 paragraphs related to a particular pattern. I have to search for those paragraphs from a log file and then print a particular line from those paragraphs. Sample: I have one file with the fixed... (3 Replies)
Discussion started by: danish0909
3 Replies

5. Shell Programming and Scripting

Script to match a pattern and print only the pattern and after that

Hi, I am writing a shell script to parse some files, and gather data. The data in the files is displayed as below. .......xyz: abz: ...... .......xyz: abz: ..... I have tried using awk and cut, bu the position of these values keep changing, so I can use awk and split it into columns. ... (14 Replies)
Discussion started by: Serena
14 Replies

6. Shell Programming and Scripting

Script to compare pattern and print a different pattern in each line

Hi, I am writing a shell script to parse some files, and gather data. The data in the files is displayed as below. .......xyz: abz: ......qrt: .... .......xyz: abz: ......qrt: ... I have tried using awk and cut, but the position of these values keep changing, so I wasn't able to get... (2 Replies)
Discussion started by: Serena
2 Replies

7. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

8. Shell Programming and Scripting

print lines up to pattern excluding pattern

11 22 33 44 55 66 77 When pattern 55 is met, print upto it, so output is 11 22 33 44 (1 Reply)
Discussion started by: anilcliff
1 Replies

9. Shell Programming and Scripting

Print a pattern between the xml tags based on a search pattern

Hi all, I am trying to extract the values ( text between the xml tags) based on the Order Number. here is the sample input <?xml version="1.0" encoding="UTF-8"?> <NJCustomer> <Header> <MessageIdentifier>Y504173382</MessageIdentifier> ... (13 Replies)
Discussion started by: oky
13 Replies

10. Shell Programming and Scripting

AWK, print no of records after pattern match.

Hi ALL :). i have a file, cat 3 + dog 5 + rat 6 - i want to print no of record having pattern "+". thanks in advance :confused:. (2 Replies)
Discussion started by: admax
2 Replies
Login or Register to Ask a Question