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?
# 8  
Old 10-02-2013
Quote:
Originally Posted by reignangel2003
It is not working under SunOS, but it is working for Cygwin.
Try it with the xpg4 sed.
# 9  
Old 10-02-2013
SunOS: And with sed under /usr/xpg4/bin/sed ?

---------- Post updated at 11:47 AM ---------- Previous update was at 11:38 AM ----------

In fact, under SunOS, the syntax is:
Code:
sed -n '/client508/{
p
n
p
n
p
}' file

Only one command by line.
Regards.
# 10  
Old 10-02-2013
Hi.
Quote:
Originally Posted by reignangel2003
i am using SunOS, it does not have -A option for grep
I sometimes use SunOS. I don't recall doing anything special when I installed it, and I have this:
Code:
OS, ker|rel, machine: SunOS, 5.10, i86pc
Distribution        : Solaris 10 10/08 s10x_u6wos_07b X86
ggrep grep (GNU grep) 2.5

So you might want to try ggrep. It is found in:
Code:
$ which ggrep
/usr/sfw/bin/ggrep

Best wishes ... cheers, drl
# 11  
Old 10-02-2013
How about good ole ex... ex -s +'/^client508/;+2 p | q!' file
# 12  
Old 10-02-2013
Quote:
Originally Posted by disedorgue
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.
Quote:
Originally Posted by disedorgue
In fact, under SunOS, the syntax is:
Code:
sed -n '/client508/{
p
n
p
n
p
}' file

Only one command by line.
I don't use SunOS, but you are almost certainly mistaken. Both commands will fail on many sed implementations (not just SunOS) because POSIX requires a semicolon before the closing }.

While we're here, POSIX does not allow a semicolon after } for further commands. For portability, you'd have to use multiple -e options.

Both of those behaviors, omitting the semicolon before the closing brace and allowing a semicolon to follow a closing brace, are extensions to POSIX on which you cannot depend if portability is important.


Also, in case it isn't intentional, sed -n '/client508/{n;n;p}' won't print the line that matches the pattern nor the line that follows the pattern.

Regards,
Alister

Last edited by alister; 10-02-2013 at 01:45 PM..
# 13  
Old 10-02-2013
Quote:
Originally Posted by alister
I don't use SunOS, but you are almost certainly mistaken. Both commands will fail on many sed implementations (not just SunOS) because POSIX requires a semicolon before the closing }.

While we're here, POSIX does not allow a semicolon after } for further commands. For portability, you'd have to use multiple -e options.

Both of those behaviors, omitting the semicolon before the closing brace and allowing a semicolon to follow a closing brace, are extensions to POSIX on which you cannot depend if portability is important.


Also, in case it isn't intentional, sed -n '/client508/{n;n;p}' won't print the line that matches the pattern nor the line that follows the pattern.

Regards,
Alister
The man posix of sed does not say quite right:
sed
For me, the best syntaxe for sed script is one command by line because all command support <newline> separator but many command do not support semicolon separator (example s command with w command).
And for <right-brace> as say the man:
Quote:
The <right-brace> shall be preceded by a <newline> and can be preceded or followed by <blank>s
Regards.
This User Gave Thanks to disedorgue For This Post:
# 14  
Old 10-02-2013
I know what POSIX says, but FWIW on Solaris this works:
Code:
sed -n '/client508/{p;n;p;n;p;}' file

as alister suggested...

and so does:
Code:
sed -n '/client508/{N;N;p;}' file

(also /usr/xpg4/bin/sed and also sed on HPUX and AIX BTW)

and probably also:
Code:
/usr/xpg4/bin/awk '/client508/' RS= file


Last edited by Scrutinizer; 10-02-2013 at 04:24 PM..
This User Gave Thanks to Scrutinizer For This Post:
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