From ken@halcyon.com Tue Jul 13 14:19:45 1999 Path: fu-berlin.de!fu-berlin.de!news.maxwell.syr.edu!newsfeed.cwix.com!206.63.63.70!nwnews.wa.com!brokaw.wa.com!ken From: ken@halcyon.com (Ken Pizzini) Newsgroups: comp.editors Subject: Re: printing a range of lines between /regexp/ +2 w/sed Date: 24 Jun 1999 01:42:07 GMT Organization: _ Lines: 41 Message-ID: References: <7kr3jb$c5u$1@nnrp1.deja.com> Reply-To: ken@halcyon.com NNTP-Posting-Host: coho.halcyon.com X-Trace: brokaw.wa.com 930188527 3519 198.137.231.21 (24 Jun 1999 01:42:07 GMT) NNTP-Posting-Date: 24 Jun 1999 01:42:07 GMT User-Agent: slrn/0.9.5.3 (UNIX) Xref: fu-berlin.de comp.editors:38291 On Wed, 23 Jun 1999 16:56:18 GMT, ASG wrote: >How to print with sed several lines which start with a line containing a >regular expression plus n lines following it? For n=5: sed -n '/a regular expression/{$!N;$!N;$!N;$!N;$!N;p;}' >I've found an example of how to print one line before and after a >regular expression: > >sed -n -e '/regexp/ {=;x;1!p;g;$!N;p;D;}' -e h > >but have a little bit of a problem deciphering it... >Can somebody help me? sed # the command -n # don't auto-print -e # next argument is a sed script (fragment) '/regexp/ #when we are on a line which matches "regexp", { #start a block of commands =; #emit the current line number to the output stream x; #exchange pattern and hold spaces (C) 1!p; #if this is not the first line, print current pattern space (B) g; #copy the hold space back to the pattern space (E) $!N; #if we are not on the last line then #append the next line to pattern space (F) p; #print the current pattern space (which may be two lines) (D) D; #delete the first (perhaps only) line from the pattern space (G) }' #done with block of commands which /regexp/ controls -e # another sed script fragment follows... h #whatever is in the pattern space now, copy it to hold space (A) So, what's going on here is that we will wind up finding the previous input line in the hold space (due to (A)) (except when we're looking at the first line, hence (B)). If the regexp matches then we temporarily swap that previous line with the current line (C) so that we can print it (B), and then we print (D) the current line and the next line (E and F). Because the N command (F) read forward a line, we make an adjustment (G) before we hold the "current" line (A) as what will be the next "previous" line. --Ken Pizzini From agozdz@my-deja.com Tue Jul 13 14:19:57 1999 Path: fu-berlin.de!fu-berlin.de!news.medicusnet.de!news.csl-gmbh.net!blackbush.xlink.net!newsfeed.germany.net!newsfeed.tli.de!newsfeed.mathworks.com!news.maxwell.syr.edu!nntp2.deja.com!nnrp1.deja.com!not-for-mail From: ASG Newsgroups: comp.editors Subject: Re: printing a range of lines between /regexp/ +2 w/sed Date: Thu, 24 Jun 1999 14:04:32 GMT Organization: Deja.com - Share what you know. Learn what you don't. Lines: 43 Message-ID: <7ktdt7$6ls$1@nnrp1.deja.com> References: <7kr3jb$c5u$1@nnrp1.deja.com> NNTP-Posting-Host: 128.96.66.159 X-Article-Creation-Date: Thu Jun 24 14:04:32 1999 GMT X-Http-User-Agent: Mozilla/4.51 [en] (WinNT; U) X-Http-Proxy: 1.0 x38.deja.com:80 (Squid/1.1.22) for client 128.96.66.159 Xref: fu-berlin.de comp.editors:38296 In article , ken@halcyon.com wrote: > On Wed, 23 Jun 1999 16:56:18 GMT, ASG wrote: > >How to print with sed several lines which start with a line containing a regular expression plus n lines following it? > > For n=5: > sed -n '/a regular expression/{$!N;$!N;$!N;$!N;$!N;p;}' > Unfortunately, it doesn't work (I'm using MKS Kornshell under NT (v. 6.2), so it's POSIX-compliant and reasonable good, right?) First, it asks for a 'do'; when I use $ for n=5: > do > sed -n '/Filed:/{$!N;$!N;$!N;p}' filename > done nothing is output, but the command $ grep 'Filed:' filename finds the line. Now, if somebody has an answer to this problem, I'd like to do this operation for a bunch of files, thus I will have to print the filename first and have a nested loop, i.e.: $ for i in *.html > do > do > echo $i > sed -n '/Filed:/{$!N;$!N;$!N;p}' $i > done > done Would that be legal, if the first problem is resolved? Tony Sent via Deja.com http://www.deja.com/ Share what you know. Learn what you don't. From randy.c.ford@bridge.bellsouth.com Tue Jul 13 14:20:03 1999 Path: fu-berlin.de!fu-berlin.de!newsfeed.nacamar.de!dispose.news.demon.net!demon!news.maxwell.syr.edu!firehose.mindspring.net!finch!atglab.bls.com!usenet From: Randy Charles Ford Newsgroups: comp.editors Subject: Re: printing a range of lines between /regexp/ +2 w/sed Date: Thu, 24 Jun 1999 14:16:31 -0500 Organization: I speak only for myself. Distribution: inet Message-ID: <3772840F.B28415E2@bridge.bellsouth.com> References: <7kr3jb$c5u$1@nnrp1.deja.com> <7ktdt7$6ls$1@nnrp1.deja.com> NNTP-Posting-Host: bstfirewall.bst.bls.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Mailer: Mozilla 4.61 [en] (WinNT; U) X-Accept-Language: en Lines: 32 Xref: fu-berlin.de comp.editors:38303 ASG wrote: > > In article , > ken@halcyon.com wrote: > > > > For n=5: > > sed -n '/a regular expression/{$!N;$!N;$!N;$!N;$!N;p;}' > > > > Unfortunately, it doesn't work (I'm using MKS Kornshell under NT (v. > 6.2), so it's POSIX-compliant and reasonable good, right?) > First, it asks for a 'do'; when I use > > $ for n=5: > > do > > sed -n '/Filed:/{$!N;$!N;$!N;p}' filename > > done In ken's post, the "For n=5:" was not meant to be entered. He was saying something like " For instance, if you wanted 5 lines, you would use the following: sed -n '/a regular expression/{$!N;$!N;$!N;$!N;$!N;p;}' " The "5" he is referring to is the five 'N's in the sed expression. -- randy randy.c.ford@bridge.bellsouth.com I speak only for myself, not for any employer or customer. From agozdz@my-deja.com Tue Jul 13 14:20:07 1999 Path: fu-berlin.de!fu-berlin.de!newshub.northeast.verio.net!hermes.visi.com!news-out.visi.com!newsfeed.enteract.com!newspeer.monmouth.com!nntp2.deja.com!nnrp1.deja.com!not-for-mail From: ASG Newsgroups: comp.editors Subject: Re: printing a range of lines between /regexp/ +2 w/sed Date: Thu, 24 Jun 1999 22:25:17 GMT Organization: Deja.com - Share what you know. Learn what you don't. Lines: 16 Message-ID: <7kub82$j3o$1@nnrp1.deja.com> References: <7kr3jb$c5u$1@nnrp1.deja.com> <7ktdt7$6ls$1@nnrp1.deja.com> <3772840F.B28415E2@bridge.bellsouth.com> NNTP-Posting-Host: 128.96.66.159 X-Article-Creation-Date: Thu Jun 24 22:25:17 1999 GMT X-Http-User-Agent: Mozilla/4.51 [en] (WinNT; U) X-Http-Proxy: 1.0 x35.deja.com:80 (Squid/1.1.22) for client 128.96.66.159 Xref: fu-berlin.de comp.editors:38308 In article <3772840F.B28415E2@bridge.bellsouth.com>, Randy Charles Ford wrote: > In ken's post, the "For n=5:" was not meant to be entered. Absolutely right--thanks! Now I'm OK. Now, how to skip some lines before printing the n-th one, or how to skip printing /regexp/ and _then_ some? Just in case you're getting impatient, I downloaded the seders manuals today, but having the books on sed and learning the tricks are two different things... Tony Sent via Deja.com http://www.deja.com/ Share what you know. Learn what you don't. From jdjohnsn@usgs.gov Tue Jul 13 14:20:35 1999 Newsgroups: comp.editors Path: fu-berlin.de!fu-berlin.de!newsfeed.nacamar.de!news.maxwell.syr.edu!u-2.maxwell.syr.edu!news1.radix.net!news.er.usgs.gov!jdjohnsn From: jdjohnsn@usgs.gov (Joel D Johnson) Subject: Re: printing a range of lines w/sed: correction X-Nntp-Posting-Host: s601dcascr.wr.usgs.gov Message-ID: Sender: news@igsrsparc2.er.usgs.gov (Janet Walz (GD) x6739) Organization: US Geological Survey References: <7kr3jb$c5u$1@nnrp1.deja.com> <7kteco$6sj$1@nnrp1.deja.com> Date: Thu, 24 Jun 1999 16:47:11 GMT Lines: 87 Xref: fu-berlin.de comp.editors:38301 Oh, I see you were using it within a script, so you shouldn't need to escape the !'s. Did you make sure you have a ";" after the p? The following should work fine: for i in *file* do echo $i sed -n '/rexpr/{$!N;$!N;$!N;$!N;p;}' $i done and you could redirect output to a file when you run the script. Note that there are only four "$!N" in the command, as each one is in addition to the original line that contained the rexpr match, for a total output of five lines. > > Your shell is probably getting in the way. Try "\" befor the "!". > > sed -n '/a regular expression/{$\!N;$\!N;$\!N;$\!N;$\!N;p;}' > > > In article <7kteco$6sj$1@nnrp1.deja.com>, ASG writes: > > In article , > > ken@halcyon.com wrote: > > > On Wed, 23 Jun 1999 16:56:18 GMT, ASG wrote: > > > >How to print with sed several lines which start with a line > > containing a regular expression plus n lines following it? > > > > > > For n=5: > > > sed -n '/a regular expression/{$!N;$!N;$!N;$!N;$!N;p;}' > > > > > > > Unfortunately, it doesn't work (I'm using MKS Kornshell under NT (v. > > 6.2), so it's POSIX-compliant and reasonable good, right?) > > First, it asks for a 'do'; when I use > > > > $ for n=5: > > > do > > > sed -n '/Filed:/{$!N;$!N;$!N;p}' filename > > > done > > > > nothing is output, but the command > > > > $ grep 'Filed:' filename > > > > finds the line. Now, if somebody has an answer to this problem, I'd like > > to do this operation for a bunch of files, thus I will have to print the > > filename first and have a nested loop, something like the _illegal_ > > script shown below: > > > > $ for i in *.html > > > do > > > do > > > echo $i > > > for i=5: > > > sed -n '/Filed:/{$!N;$!N;$!N;p}' $i > > > done > > > done > > > > Any ideas, please? > > > > Tony > > > > > > > > Sent via Deja.com http://www.deja.com/ > > Share what you know. Learn what you don't. > > -- > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ > Joel D. Johnson (jdjohnsn@usgs.gov) > U.S. Geological Survey, Water Resources Division > Placer Hall, 6000 J Street > Sacramento, CA 95819-6129 > (916) 278-3163 > "The chief weapons of UNIX: Fear, surprise and ruthless efficiency." -- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Joel D. Johnson (jdjohnsn@usgs.gov) U.S. Geological Survey, Water Resources Division Placer Hall, 6000 J Street Sacramento, CA 95819-6129 (916) 278-3163 "The chief weapons of UNIX: Fear, surprise and ruthless efficiency." From agozdz@my-deja.com Tue Jul 13 14:20:41 1999 Path: fu-berlin.de!fu-berlin.de!newsfeed.mathworks.com!news.maxwell.syr.edu!nntp2.deja.com!nnrp1.deja.com!not-for-mail From: ASG Newsgroups: comp.editors Subject: Re: printing a range of lines w/sed: correction Date: Thu, 24 Jun 1999 22:19:03 GMT Organization: Deja.com - Share what you know. Learn what you don't. Lines: 27 Message-ID: <7kuase$itu$1@nnrp1.deja.com> References: <7kr3jb$c5u$1@nnrp1.deja.com> <7kteco$6sj$1@nnrp1.deja.com> NNTP-Posting-Host: 128.96.66.159 X-Article-Creation-Date: Thu Jun 24 22:19:03 1999 GMT X-Http-User-Agent: Mozilla/4.51 [en] (WinNT; U) X-Http-Proxy: 1.0 x24.deja.com:80 (Squid/1.1.22) for client 128.96.66.159 Xref: fu-berlin.de comp.editors:38307 In article , jdjohnsn@usgs.gov (Joel D Johnson) wrote: > > Oh, I see you were using it within a script, so you shouldn't need to > escape the !'s. Did you make sure you have a ";" after the p? The > following should work fine: > > for i in *file* > do > echo $i > sed -n '/rexpr/{$!N;$!N;$!N;$!N;p;}' $i > done My mistake was to use the n=5: statement, which I misunderstood from Ken's post. It only shows my level of familiarity with Unix scripts, right?-- but I'm a chemist... Anyway, everything works fine now-- thanks for your and Ken's help. Now, how to print a line matching /regexp/ and the second one following it (skip the first following), or only the third line following /regexp/? I tried various combinations of d, but with no success... Tony Sent via Deja.com http://www.deja.com/ Share what you know. Learn what you don't. From ken@halcyon.com Tue Jul 13 14:20:46 1999 Path: fu-berlin.de!fu-berlin.de!news.maxwell.syr.edu!newsfeed.cwix.com!206.63.63.70!nwnews.wa.com!brokaw.wa.com!ken From: ken@halcyon.com (Ken Pizzini) Newsgroups: comp.editors Subject: Re: printing a range of lines w/sed: correction Date: 25 Jun 1999 04:57:41 GMT Organization: _ Lines: 10 Message-ID: References: <7kr3jb$c5u$1@nnrp1.deja.com> <7kteco$6sj$1@nnrp1.deja.com> <7kuase$itu$1@nnrp1.deja.com> Reply-To: ken@halcyon.com NNTP-Posting-Host: coho.halcyon.com X-Trace: brokaw.wa.com 930286661 10713 198.137.231.21 (25 Jun 1999 04:57:41 GMT) NNTP-Posting-Date: 25 Jun 1999 04:57:41 GMT User-Agent: slrn/0.9.5.3 (UNIX) Xref: fu-berlin.de comp.editors:38312 On Thu, 24 Jun 1999 22:19:03 GMT, ASG wrote: >Now, how to print a line matching /regexp/ and the second one following >it (skip the first following), or only the third line following >/regexp/? I tried various combinations of d, but with no success... Yeah; the "d" command's semantics aren't as useful as they might be. Here, try this idiom: sed -n '/regexp/{p;n;n;p;}' --Ken Pizzini From era@allenhome.sky.net Tue Jul 13 14:20:59 1999 Path: fu-berlin.de!fu-berlin.de!arclight.uoregon.edu!logbridge.uoregon.edu!uunet!sea.uu.net!dfw.uu.net!ffx.uu.net!alpha.sky.net!allenhome.sky.net!era Newsgroups: comp.editors Subject: Re: printing a range of lines w/sed: correction References: <7kr3jb$c5u$1@nnrp1.deja.com> <7kuase$itu$1@nnrp1.deja.com> Organization: I am at home here. From: era@allenhome.sky.net (Ed Allen) Message-ID: Lines: 14 Date: Fri, 25 Jun 1999 01:34:07 -0500 NNTP-Posting-Host: 209.90.4.76 X-Complaints-To: abuse@sky.net X-Trace: alpha.sky.net 930323404 209.90.4.76 (Fri, 25 Jun 1999 10:10:04 CDT) NNTP-Posting-Date: Fri, 25 Jun 1999 10:10:04 CDT Xref: fu-berlin.de comp.editors:38316 In article <7kuase$itu$1@nnrp1.deja.com>, ASG wrote: > >Now, how to print a line matching /regexp/ and the second one following >it (skip the first following), or only the third line following >/regexp/? I tried various combinations of d, but with no success... > sed -n -e '/regexp/{p;n;n;p;}' in_file Will print the /regexp/ line, the first 'n;' will skip a line and 'n;p;' will will print the second one following. Add more 'n;' to skip more. The '-n' argument to sed suppresses printing unless sed is given a 'p' command. The ';' are seperators for sed commands. From epement@jpusa.chi.il.us Tue Jul 13 14:21:19 1999 Path: fu-berlin.de!fu-berlin.de!news-europe.mathworks.com!newsfeed.mathworks.com!newsfeed.enteract.com!news.enteract.com!not-for-mail From: epement@jpusa.chi.il.us (Eric Pement) Newsgroups: comp.editors Subject: Re: printing a range of lines w/sed: correction Date: Thu, 08 Jul 1999 22:53:22 GMT Organization: EnterAct L.L.C. Turbo-Elite News Server Lines: 60 Message-ID: <37852a17.6570828@news.jpusa.net> References: <7kr3jb$c5u$1@nnrp1.deja.com> <7kuase$itu$1@nnrp1.deja.com> <7l8gek$p9e$1@nnrp1.deja.com> NNTP-Posting-Host: 216.80.26.176 X-Newsreader: Forte Free Agent 1.11/32.235 Xref: fu-berlin.de comp.editors:38549 On Mon, 28 Jun 1999 18:55:23 GMT, ASG wrote: >In article , > era@allenhome.sky.net (Ed Allen) wrote: >> sed -n -e '/regexp/{p;n;n;p;}' in_file >> >> Will print the /regexp/ line, the first 'n;' will skip a line and >> 'n;p;' will will print the second one following. Add more 'n;' to >> skip more. > >Exactement. I'm assuming (will try soon) that > >sed -n -e '/abc/,/xyz/{p;n;n;n;p;}' in_file > >would work for a range of lines between /abc/ and /xyz/? > >Tony I haven't seen your previous discussion, but if you're wanting to print a cyclic pattern of lines within a specific range (like "/abc/,/xyz/"), then the suggestion above will not work reliably. I've tested it, and it does print lines outside the range brackets. You'd be better off using GNU sed and the special M~N commands, where M is a line to start with and N is a number of lines to skip or jump over. Thus, gsed -n '/abc/,/xyz/{1~3p;}' in_file appears to do what you want. For more info on the M~N command, which is works for GNU sed vers. 2.05 and higher, let me quote from the sed FAQ: BEGIN~STEP selection: GNU sed can select a series of lines in the form M~N, where M and N are integers (with gsed v2.05, M must be less than N). Beginning at line M (M may equal 0), every Nth line is selected. Thus, gsed '1~3d' file # delete every 3d line, starting with line 1 # deletes lines 1, 4, 7, 10, 13, 16, ... gsed -n '2~5p' file # print every 5th line, starting with line 2 # prints lines 2, 7, 12, 17, 22, 27, ... With gsed v3.02, M may be any valid line number. With gsed v2.05, if M is greater than or equal to N (the STEP value), nothing will be selected, except in one pointless case, 0~0, which selects every line. Hope this helps. Kind regards, Eric Pement -- Eric Pement senior editor, Cornerstone magazine Info on SED here: http://www.cornerstonemag.com http://www.cornerstonemag.com/sed 939 W. Wilson, Chicago, IL 60640 tel: 773/561-2450, 1-(ext.)2084 fax: 773/989-2076