-- Eat Healthy | _ _ | Nothing would be done at all, Stay Fit | _at_ _at_ | If a man waited to do it so well, Die Anyway | v | That no one could find fault with it. demas_at_theworld.com | \___/ | http://world.std.com/~cpd ---------------------------------------------------------------------- Subject: Re: inserting lines before and after lines with matching pattern using sed? From: Ed Morton <mortonAVOIDINGSPAM_at_Lucent.com> Newsgroups: comp.unix.shell Date: Tue, Jul 22, 2003 3:37 PM Message-ID: <3F1D9273.9030505_at_Lucent.com> On 7/22/2003 11:21 AM, Charles Demas wrote: > In article <9cc83d20.0307220718.56ad7182_at_posting.google.com>, > Rob Eger <reger_at_txcorp.com> wrote: > <snip> > It can probably be done with sed, but doing it with awk is easier, IMO. > > Untested code: > > awk '/certain string/ {print "Before stuff"; print; > print "After stuff"; next} > {print}' infile > outfile > > or > > awk '/certain string/ {print "Before stuff"} > {print} > /certain string/ {print "After stuff"}' infile > outfileprint > Both fine, though I'd probably do it this way in awk: awk '/certain string/ {b="Before stuff\n";a="\nAfter stuff"} {print b $0 a;b=a=""}' or: awk '{o=$0} /certain string/ {o="Before stuff\n"o"\nAfter stuff"} {print o}' or (in a ?awk where you can set $0): gawk '/certain string/ {$0="Before stuff\n"$0"\nAfter stuff"} {print}' Regards, Ed. > > Chuck Demas > ---------------------------------------------------------------------- Subject: Re: inserting lines before and after lines with matching pattern using sed? From: Damian Ibbotson <member9705_at_dbforums.com> Newsgroups: comp.unix.shell Date: Tue, Jul 22, 2003 12:40 PM Message-ID: <3139156.1058892012_at_dbforums.com> awk '/PATTERN/ {print "header"; print; print "trailer"; next} {print}' yourFile -- Posted via http://dbforums.com ---------------------------------------------------------------------- Subject: Re: inserting lines before and after lines with matching pattern using sed? From: sharma__r_at_hotmail.com (rakesh sharma) Newsgroups: comp.unix.shell Date: Tue, Jul 22, 2003 4:48 PM Message-ID: <ed24e4cf.0307221248.16f109cd_at_posting.google.com> reger_at_txcorp.com (Rob Eger) wrote in message news:<9cc83d20.0307220718.56ad7182_at_posting.google.com>... > I need to go through a file and wherever I find lines that contain a > certain string I need to insert a line above and below that line. > Lines that don't contain the string can go straight into the new file. > > I've been trying various sed commands to no avail. Can this be done > with sed, or am I wasting my time and need to go at it a different > way? If it can be done with sed, how? > > Thanks, > Rob Eger > > Tech-X Corporation > Boulder, CO sed -e ' /string/i\ BEFORE /string/a\ AFTER ' inputfile ( I am assuming you are using bourne shell. C-shell quoting would be different) ---------------------------------------------------------------------- Subject: Re: inserting lines before and after lines with matching pattern using sed? From: sharma__r_at_hotmail.com (rakesh sharma) Newsgroups: comp.unix.shell Date: Tue, Jul 22, 2003 10:20 PM Message-ID: <ed24e4cf.0307221435.4e72d125_at_posting.google.com> reger_at_txcorp.com (Rob Eger) wrote in message news:<9cc83d20.0307220718.56ad7182_at_posting.google.com>... > I need to go through a file and wherever I find lines that contain a > certain string I need to insert a line above and below that line. > Lines that don't contain the string can go straight into the new file. > > I've been trying various sed commands to no avail. Can this be done > with sed, or am I wasting my time and need to go at it a different > way? If it can be done with sed, how? > > Thanks, > Rob Eger > > Tech-X Corporation > Boulder, CO sed -e '/regex/!b i\ BEFORE a\ AFTER' inputfile or, sed -e '/regex/!b s/^/BEFORE\ /;s/$/\ AFTER/' inputfile (note: there should be nothing after the trailing \ above, as they are there to escape the newline) using nawk: nawk '/regex/{$0="BEFORE\n"$0"\nAFTER"}1' inputfile or with perl: perl -ple '$_="BEFORE\n$_\nAFTER"if/regex/' inputfile RichGlazier_at_netscape.net (Rich Glazier) wrote: >Tru64 5.1A PK5 > >Does anybody know how to insert a blank line above a specific occurance in a file, without using vi? As in the example file below, I want to insert a balnk line above every occurance of a line starting (or containing) "hello". I tried different itterations of tr, or sed, but most utilities deal with "lines following", not "lines prior". > >change: > >hello there >tru64 managers >my name is rich >glazier 1234 1234 >hello there >tru64 managers >my name is rich >glazier1 12345 12345 >hello there >tru64 managers >my name is rich >glazier2 12346 12346 > >to: > > >hello there >tru64 managers >my name is rich >glazier 1234 1234 > >hello there >tru64 managers >my name is rich >glazier1 12345 12345 > >hello there >tru64 managers >my name is rich >glazier2 12346 12346 > > >__________________________________________________________________ >McAfee VirusScan Online from the Netscape Network. >Comprehensive protection for your entire computer. Get your free trial today! >http://channels.netscape.com/ns/computing/mcafee/index.jsp?promo=393397 > >Get AOL Instant Messenger 5.1 free of charge. Download Now! >http://aim.aol.com/aimnew/Aim/register.adp?promo=380455 > __________________________________________________________________ McAfee VirusScan Online from the Netscape Network. Comprehensive protection for your entire computer. Get your free trial today! http://channels.netscape.com/ns/computing/mcafee/index.jsp?promo=393397 Get AOL Instant Messenger 5.1 free of charge. Download Now! http://aim.aol.com/aimnew/Aim/register.adp?promo=380455Received on Wed Oct 29 2003 - 18:26:12 NZDT
This archive was generated by hypermail 2.4.0 : Wed Nov 08 2023 - 11:53:44 NZDT