Printing next two lines from a file after grepping a specific pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Printing next two lines from a file after grepping a specific pattern
# 1  
Old 07-16-2013
Printing next two lines from a file after grepping a specific pattern

Hi

I have a file like
Code:
# vi require.txt
1,BANK,Read blocks that cycle.
yellow
Read blocks.
2,ACCOUNT,Finished
Red
Finished .
3,LOAN, pipe
white
pipe
4,PROFIT,Resolve.
black
Resolve

Am using like
Code:
cat require.txt | grep -w ACCOUNT

The output I get is
Code:
2,ACCOUNT,Finished

But i need the output as

Code:
2,ACCOUNT,Finished
Red
Finished .

I need to grep the next two lines from that file once the grep patterns is matched..

Please help Smilie
# 2  
Old 07-16-2013
My fav for this is..

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=0 a=2 s="ACCOUNT" require.txt

b is number of lines above pattern and a is number of line below the pattern s is the search pattern

If you have GNU grep

Code:
grep -A 2 ACCOUNT require.txt

This User Gave Thanks to vidyadhar85 For This Post:
# 3  
Old 07-16-2013
Code:
 sed -n "/ACCOUNT/{N;N;p;}" file

# 4  
Old 07-16-2013
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=0 a=2 s="ACCOUNT" require.txt

b is number of lines above pattern and a is number of line below the pattern s is the search pattern

Thanks this code works very fine Smilie
# 5  
Old 07-16-2013
Code:
awk '/ACCOUNT/{a=FNR}; (a>0 && FNR<=a+2){print}' file

# 6  
Old 07-16-2013
Try
Code:
 awk '/ACCOUNT/ {L=3} L-->0' file
2,ACCOUNT,Finished
Red
Finished .

# 7  
Old 07-18-2013
Code:
# vi require.txt 
1,BANK,Read blocks that cycle.
 yellow 
Read blocks.
 2,ACCOUNT,Finished 
Red 
Finished . 
3,LOAN, pipe 
white 
pipe 
4,PROFIT,Resolve. 
black 
Resolve
5,PROFITIABLE,Loss
orange
Loss
6,ACCOUNTANT,clerk
yellow
work

If i use the 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=0 a=2 s="ACCOUNT" require.txt

The output is
2,ACCOUNT,Finished
Red
Finished .
6,ACCOUNTANT,clerk
yellow
work


This grep all pattern relative to ACCOUNT like ACCOUNT and ACCOUNTANT are grepped..

Please suggest me a way to grep the exact pattern ACCOUNT alone from this file
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match all lines in file where specific text pattern is less than

In the below file I am trying to grep or similar, all lines where only AF= is less than 0.4.. Thank you :). grep grep "AF=" ,+ .4 file file 12 112036782 . T C 34.0248 PASS ... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

3. UNIX for Dummies Questions & Answers

Printing lines with specific strings at specific columns

Hi I have a file which is tab-delimited. Now, I'd like to print the lines which have "chr6" string in both first and second columns. Could anybody help? (3 Replies)
Discussion started by: a_bahreini
3 Replies

4. Shell Programming and Scripting

Grepping a specific row from a file

Hi I have output of a command saved in a file.. # cat /file.txt System: cu=4 ent=0.1 mode=on cu min u s w i 0 500 0.1 0.3 0.5 0.1 1 200 0.5 0.2 0.3 0.0 By using ksh, what I need to do is, I need to grep the u,s,w and i... (5 Replies)
Discussion started by: Priya Amaresh
5 Replies

5. UNIX for Advanced & Expert Users

Need command for grepping pattern lines with subsequent lines

Hi, I have a requirement like, I have a list of pattens in a file say pattern.txt, PHC111 PHC113 and in another file called master.lst i have entries like, PHC111 a b PHC112 a PHC113 b c PHC114 d e (5 Replies)
Discussion started by: rbalaj16
5 Replies

6. Shell Programming and Scripting

Printing all lines before a specific string and a custom message 2 lines after

Hello all, I need to print all the lines before a specific string and print a custom message 2 lines after that. So far I have managed to print everything up the string, inclusively, but I can't figure out how to print the 2 lines after that and the custom message. My code thus far is:... (4 Replies)
Discussion started by: SEinT
4 Replies

7. Shell Programming and Scripting

Printing several lines of a file after a specific expression

Hello, I want to print a number of lines of a file after a specific expression of a line. I have this sed command but it prints only 1 line after the expression. How could I adapt it to print for instance 10 lines after or 15 lines after ? sed -n '/regexp/{n;p;}' Thx & Regs, Rany. (5 Replies)
Discussion started by: rany1
5 Replies

8. Shell Programming and Scripting

merging of 2 consecutive lines in a file for a specific pattern

Hi , I'm looking for a way to merge two lines only for a given pattern / condition. Input : abcd/dad + -49.201 2.09 -49.5 34 ewrew rewtre * fdsgfds/dsgf/sdfdsfasdd + -4.30 0.62 -49.5 45 sdfdsf cvbbv * sdfds/retret/asdsaddsa + ... (1 Reply)
Discussion started by: novice_man
1 Replies

9. Shell Programming and Scripting

how to delete lines from a file which starts with a specific pattern

I need to delete those lines from a file, which starts with 45. How to do it? (3 Replies)
Discussion started by: mady135
3 Replies

10. Shell Programming and Scripting

Printing lines with specific awk NF

I have this files: ./frm/lf_mt1_cd.Ic_cell_template.attr ./die/addgen_tb_pumd.Ic_cell_template.attr ./min_m1_n.Ic_cell_template.attr When I use: awk -F\/ '{print NF}' Would result to: 3 3 2 I would like to list the files with 3 fields on it. Any Suggestions? (1 Reply)
Discussion started by: jehrome_rando
1 Replies
Login or Register to Ask a Question