How to get next 3 lines after regex?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get next 3 lines after regex?
# 1  
Old 10-02-2013
How to get next 3 lines after regex?

HI,

Just wanted to get some advice on how i can get the next 3 lines after a regex:

Say i have a file which contains something like below:

Code:
client467
        master tcp ether tor0141 4467
        query  tcp ether tor0141 4467

client468
        master tcp ether tor2141 4468
        query  tcp ether tor2141 4468

client490
        master tcp ether tor3147 4490
        query  tcp ether tor3147 4490

client508
        master tcp ether tor3154 4508
        query  tcp ether tor154 4508

so once i search for the word say "client508" , it will give me an output of

Code:
client508
        master tcp ether tor3154 4508
        query  tcp ether tor154 4508

Thanks,
# 2  
Old 10-02-2013
If you have GNU grep then it has an 'after context' option.
Code:
grep -A2 client508 file

# 3  
Old 10-02-2013
i am using SunOS, it does not have -A option for grep
# 4  
Old 10-02-2013
Code:
awk '/client508/ {for (i=0;i<3;i++) {print;getline}}' file

(You may need to use nawk or /usr/xpg4/bin/awk)
This User Gave Thanks to CarloM For This Post:
# 5  
Old 10-02-2013
Hi,
This command should work:
Code:
sed -n '/client508/{n;n;p}' file

or working as grep -A2:
Code:
sed -n '/client508/{p;n;p;n;p}' file

Regards.
# 6  
Old 10-02-2013
Code:
awk '/client508/ {a=3} a {print;a--}' file
client508
        master tcp ether tor3154 4508
        query  tcp ether tor154 4508

# 7  
Old 10-02-2013
Hi disedorgue,

It is not working under SunOS, but it is working for Cygwin.

In SunOs
Code:
sed: command garbled: /client508/{p;n;p;n;p}/

CYGWIN
Code:
client508
        master tcp ether tor0pod154 4508
        query  tcp ether tor0pod154 4508

Thanks,
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sed/awk to delete a regex between range of lines

Hi Guys I am looking for a solution to one problem to remove parentheses in a range of lines. Input file module bist_logic_inst(a, ab , dhd, dhdh , djdj, hdh, djjd, jdj, dhd, dhp, dk ); input a; input ab; input dhd; input djdj; input dhd; output hdh; output djjd; output jdj;... (5 Replies)
Discussion started by: kshitij
5 Replies

2. Shell Programming and Scripting

Grepping or awking multiple lines in a file - regex

data.txt: hellohellohello mellomello1mello tellotellotellotello bellobellowbellow vellow My attempts: egrep ".*mello1\n.*bellow" data.txt awk '/.*mello1.*\nbellow/' data.txt how can i search for patterns that are on different lines using simple egrep or awk? i only want the... (7 Replies)
Discussion started by: SkySmart
7 Replies

3. Shell Programming and Scripting

To print lines between 2 timestamps using awk|sed and regex

Hi, I am using the following code to fetch lines that are generated in last 1 hr . Hence, I am using date function to calculate -last 1 hr & the current hr and then somehow use awk (or sed-if someone could guide me better) with some regex pattern. dt_1=`date +%h" "%d", "%Y\ %l -d "1 hour... (10 Replies)
Discussion started by: sarah-alikhan31
10 Replies

4. Shell Programming and Scripting

Filter (by max length) only lines not matching regex

I have a large file of many pairs of sequences and their headers, which always begin with '>' I'm looking for help on how to retain only sequences (and their headers) below a certain length. So if min length was 10, output would be I can filter by length, but I'm not sure how to exclude... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

5. Shell Programming and Scripting

Print lines that match regex on xth string

Hello, I need an awk command to print only the lines that match regex on xth field from file. For example if I use this command awk -F"|" ' $22 == "20130117090000.*" 'It wont work, I think, because single quotes wont allow the usage of the metacharacter star * . On the other hand I dont know... (2 Replies)
Discussion started by: black_fender
2 Replies

6. Shell Programming and Scripting

regex matches from lines in file

Hello, I try to script something (bash-script) and can not find a way to get and store a match into a variable from a line in a file. grep isn't useful as the matches are not returned - just colored. I can't get 'expr' to work for me. Is it necessary to use a perl-script with regex instead? ... (7 Replies)
Discussion started by: daWonderer
7 Replies

7. Shell Programming and Scripting

Extract Log lines with Thread-(regex)

Hi everyone, Fist of all I must confess that I am pretty new in the Unix environment and especially to shell scripting, however due to work related requirements I have started to analyze software specific logs. The logs are structured so that it records by sessionID AND/OR Thread number, the... (3 Replies)
Discussion started by: sushimatt
3 Replies

8. Shell Programming and Scripting

Perl Regex matching multiple lines

I need a way to extract data from X 4T Solution 21 OCT 2011 37 .00 to account 12345678 User1 user2 X 4T Solution Solution Unlimited 11 Sep 2009 248 .00 to account 87654321 user3 user4 I need it to extract 'X' '37.00' and account number 12345678. I have extracted above stuff... (3 Replies)
Discussion started by: chakrapani
3 Replies

9. Shell Programming and Scripting

Print lines after regex

Hello, I need to print four lines inmediatly after the regexp, but not the line containing the regexp. The print should show the four lines together in one. Thanks! (13 Replies)
Discussion started by: auratus42
13 Replies

10. Shell Programming and Scripting

Grep using REGEX that spans several lines

Hi all, I need help regarding the following: I need to support text search (number of text occurrences), in user specified - text file and - 'REGULAR EXPRESSION'. >grep -c 'pattern' fileToSearch was working successfully until I was requested to SUPPORT "\n"(new line separator in user... (1 Reply)
Discussion started by: epro
1 Replies
Login or Register to Ask a Question