sed read X lines after match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed read X lines after match
# 1  
Old 01-18-2013
Question sed read X lines after match

I want to do something like sed -n '/PATTERN/,+10p' and get the ten lines following PATTERN. However, this throws an "expected context address" with the sed that comes with OSX Lion. If that + is a GNUism, can I do this, or do I have to find another tool?
# 2  
Old 01-18-2013
10 following the pattern, excluding the pattern:
Code:
awk 'c&&c--;/pattern/{c=10}' infile

10 following the pattern, including the pattern:
Code:
awk '/pattern/{c=11};c&&c--' infile

# 3  
Old 01-18-2013
The obvious way is to match a line, then use the "n" command to read more lines and finally output them with the "p" command (adjust the number of "n"s to match your goal):

Code:
sed -n '/<match-regexp>/ { n, n, p }' /path/to/infile

Another possibility would be a "range clause": ranges start with a starting regexp and end either with an ending regexp or a line number, which can be absolute (simply a number) or relative to the file position. This is what ".,$ ..." is: from the current line (".") to the end of the file ("$"). The blueprint is:

Code:
/<start-regexp>/,/<end-regexp>/ {cmd, cmd, ... }

It is possible in "vi" to use relative line numbers like you did:

Code:
/<regexp>/,+10 {cmd}

but i doubt this is standard BRE (would have to consult the reference, which i haven't access to right now). The above solution is guaranteed to work with every "sed", though.

By the way, check the man page of your "grep". Some versions have the feature you look for: they can match a line and then output a configurable mount of lines before and/or after the match.

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed; insert text two lines above match

Hi! Considering below text, how would I use sed to insert text right below the v0005-line, using the SEPARATOR-line as a pattern to search for, so two lines above the separator? I can do it right above the separator, but not 2 lines... # v0004 - Some text # v0005 - More text #... (5 Replies)
Discussion started by: indo1144
5 Replies

2. UNIX for Dummies Questions & Answers

sed, join lines that do not match pattern

Hello, Could someone help me with sed. I have searched for solution 5 days allready :wall:, but cant find. Unfortunately my "sed" knowledge not good enough to manage it. I have the text: 123, foo1, bar1, short text1, dat1e, stable_pattern 124, foo2, bar2, long text with few lines, date,... (4 Replies)
Discussion started by: petrasl
4 Replies

3. Shell Programming and Scripting

Sed delete blank lines upto first pattern match

Hi Im trying to do the following in sed. I want to delete any blank line at the start of a file until it matches a pattern and then stops. for example: Input output: I have got it to work within a range of two patterns with the following: sed '/1/,/pattern/{/^]*$/d}' The... (2 Replies)
Discussion started by: duonut
2 Replies

4. Shell Programming and Scripting

Insert few lines above a match using sed, and within a perl file.

Greetings all, I am trying to match a string, and after that insert a few lines above that match. The string is "Version 1.0.0". I need to insert a few lines ONLY above the first match (there are many Version numbers in the file). The rest of the matches must be ignored. The lines I need to... (2 Replies)
Discussion started by: nagaraj s
2 Replies

5. Shell Programming and Scripting

sed print all lines after pattern match

HiCan someone show me how to print all lines from a file after a line matching a pattern using sed?Thanks (13 Replies)
Discussion started by: steadyonabix
13 Replies

6. Shell Programming and Scripting

sed problem - delete all lines until a match on 2 lines

First of all, I know this can be more eassily done with perl or other scripting languages but, that's not the issue. I need this in sed. (or wander if it's possible ) I got a file (trace file to recreate the control file from oracle for the dba boys) which contains some lines another line... (11 Replies)
Discussion started by: plelie2
11 Replies

7. Shell Programming and Scripting

swapping lines that match a condition using sed, perl or the like

I'm a bit new to regex and sed/perl stuff, so I would like to ask for some advice. I have tried several variations of scripts I've found on the net, but can't seem to get them to work out just right. I have a file with the following information... # Host 1 host 45583 { filename... (4 Replies)
Discussion started by: TheBigAmbulance
4 Replies

8. Shell Programming and Scripting

SED printing just parts of lines that match an expression

Hi - I am guessing this is fairly simple for someone .. but I can not quite figure it out. I need a sed command to print just parts of lines from a file. e.g. filea.txt 4710451 : Success : MODIFY : cn=user1,dc=org,dc=uk Message log started Message log ended 4710452 : Success : MODIFY :... (7 Replies)
Discussion started by: sniper57
7 Replies

9. Shell Programming and Scripting

read and match multiple lines in perl

Could any one tell me how to read and match multiple lines in perl? Did this code below still work in this situation? while (<FILE>) { if (/ /) { } } Thanks a lot! (5 Replies)
Discussion started by: zx1106
5 Replies

10. Shell Programming and Scripting

SED: match pattern & delete matched lines

Hi all, I have the following data in a file x.csv: > ,this is some text here > ,,,,,,,,,,,,,,,,2006/11/16,0.23 > ,,,,,,,,,,,,,,,,2006/12/16,0.88 < ,,,,,,,,,,,,,,,,this shouldnt be deleted I need to use SED to match anything with a > in the line and delete that line, can someone help... (7 Replies)
Discussion started by: not4google
7 Replies
Login or Register to Ask a Question