|
Re: Stingily extracting a block of lines: msg#00019editors.sed.user
On Thursday 08 December 2005 02:38, you wrote: > Entity Angus Leeming spoke thus: > >> I want to extract a range of lines beginning with the word 'bundle' and > >> ending with the word '.eml,'. The 'bundle' delimiter will occur > >> repeatedly throughout the file, so I want to use the most recent > >> instance (greedy). > >> > >> How would I do it using sed? > > > > You're being too vague. Guessing that your data looks like > > > > the beginning > > foo > > foo bundle bar 1 > > bar > > baz > > foo bundle baz 2 > > noise > > noise > > bar .elm > > noise > > the end > > > > Then the sed script is simply: > > > > sed -n '/bundle/,/\.elm/p' input.txt > output.txt > > Yes, that is essentially it, but your script returns lines from the first > instance of bundle, and I want it to return lines starting from the last > instance in the file. Then the solution is a little more involved. Logically, it becomes: Step 1. Store all lines from the first "bundle" to ".elm" in the hold space. Step 2. Move this hold space to the pattern space for further processing. Step 3. Remove all lines up to the final "bundle". Use the sed script below, trial.sed, as sed -n -f trial.sed input.txt > output.txt Regards, Angus /bundle/,/\.elm/{ # Copy all lines from the first "bundle" to the first ".elm" # into the hold space. H # On reaching ".elm"... /\.elm/{ # Copy the contents of the hold space back into the pattern space. g # If the pattern space contains more than one "bundle" line # then strip everything up to the beginning of the final # "bundle" line. /bundle[^\n]*\n.*bundle/s/^.*\n\([^\n]*bundle\)/\1/ # Don't forget to print... p } } ------------------------ Yahoo! Groups Sponsor --------------------~--> Get Bzzzy! (real tools to help you find a job). Welcome to the Sweet Life. http://us.click.yahoo.com/KIlPFB/vlQLAA/TtwFAA/dkFolB/TM --------------------------------------------------------------------~-> -- Yahoo! Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/sed-users/ <*> To unsubscribe from this group, send an email to: sed-users-unsubscribe@xxxxxxxxxxxxxxx <*> Your use of Yahoo! Groups is subject to: http://docs.yahoo.com/info/terms/ |
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| Previous by Date: | Re: Simple Conditional execution in a script: 00019, YogS |
|---|---|
| Next by Date: | Re: Stingily extracting a block of lines: 00019, kaushik . m |
| Previous by Thread: | Re: Stingily extracting a block of linesi: 00019, Angus Leeming |
| Next by Thread: | Re: Stingily extracting a block of lines: 00019, kaushik . m |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |