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?
# 15  
Old 10-02-2013
Quote:
Originally Posted by Scrutinizer
I know what POSIX says ...
Obviously, I did not (but now do). Somehow I had internalized the semicolon before the right-brace as POSIX instead of merely historical practice.

Regards,
Alister
# 16  
Old 10-02-2013
In the rationale section for sed there is also this :

Quote:
Historically, the sed ! and } editing commands did not permit multiple commands on a single line using a <semicolon> as a command delimiter. Implementations are permitted, but not required, to support this extension.
I find that on POSIX compliant systems the semicolon before closing brace seems to always work with sed implementations.

Last edited by Scrutinizer; 10-02-2013 at 05:00 PM..
These 3 Users Gave Thanks to Scrutinizer For This Post:
# 17  
Old 10-02-2013
Quote:
Originally Posted by Scrutinizer
I know what POSIX says, but FWIW on Solaris this works:
Code:
sed -n '/client508/{p;n;p;n;p;}' file

as alister suggested...
Interesting...
I try to write a doc on sed (in french because my english is really very bad Smilie ), and i want describe the best practice.
Quote:
Originally Posted by Scrutinizer
and so does:
Code:
sed -n '/client508/{N;N;p;}' file

Lines will not print if pattern found in last or second last line of file.

Otherwise, sed solution is not the best solution for this problem because we can't chose the number line easily.

Regards.
# 18  
Old 10-02-2013
Quote:
Originally Posted by disedorgue
Lines will not print if pattern found in last or second last line of file.
If those boundary conditions are a concern:
Code:
sed -n '/client508/{$!N; $!N; p;}' file

Regards,
Alister
This User Gave Thanks to alister 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