SUMMARY: inserting a blank line in a file

From: Rich Glazier (RichGlazier@netscape.net)
Date: Wed Oct 29 2003 - 13:24:55 EST


Thanks to everyone who responded, I and I think everyone did! That was great. I got variations on the same awk, sed, and perl routines. I have attached several examples of the awk and sed ideas. Also, you may want to cross reference the lp script summary- that is where i actually use the awk example.

Thanks again.

Try this:
sed 's/hello there/\
hello there/' filename
You actually hit Enter after the \ and then finish the command on the next line.
HTH,
Trevor Osatchuk

//////////////////////////////////////

sed -e 's/^hello/\
hello/g' file.txt > output.txt

that should do it for you.

hope that helps,
-charlie

//////////////////////////////////

This was passwd on by Payton Bland from a Unix scripting list...

/////////////////////////////////

Subject: inserting lines before and after lines with matching pattern
using sed?
From: reger@txcorp.com (Rob Eger)
Newsgroups: comp.unix.shell
Date: Tue, Jul 22, 2003 11:18 AM
Message-ID: <9cc83d20.0307220718.56ad7182@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

----------------------------------------------------------------------

Subject: Re: inserting lines before and after lines with matching
pattern using sed?
From: Ed Morton <mortonAVOIDINGSPAM@Lucent.com>
Newsgroups: comp.unix.shell
Date: Tue, Jul 22, 2003 12:14 PM
Message-ID: <3F1D62E2.7050308@Lucent.com>

On 7/22/2003 10:18 AM, Rob Eger wrote:
> 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?

This:

sed -e '/a certain string/{
s/^/X\
/
s/$/\
Y/
}
'

Will put an "X" on the line before every occurrence of "a certain string" and a
"Y" on the succeeding line.

    Ed.

----------------------------------------------------------------------

Subject: Re: inserting lines before and after lines with matching
pattern using sed?
From: demas@TheWorld.com (Charles Demas)
Newsgroups: comp.unix.shell
Date: Tue, Jul 22, 2003 12:21 PM
Message-ID: <bfjoaj$hg8$1@pcls4.std.com>

In article <9cc83d20.0307220718.56ad7182@posting.google.com>,
Rob Eger <reger@txcorp.com> wrote:
>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?

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

Chuck Demas

-- 
   Eat Healthy        |   _ _   | Nothing would be done at all,
   Stay Fit           |   @ @   | If a man waited to do it so well,
   Die Anyway         |    v    | That no one could find fault with it.
   demas@theworld.com |  \___/  | http://world.std.com/~cpd
----------------------------------------------------------------------
Subject: Re: inserting lines before and after lines with matching 
pattern using sed?
From: Ed Morton <mortonAVOIDINGSPAM@Lucent.com>
Newsgroups: comp.unix.shell
Date: Tue, Jul 22, 2003 3:37 PM
Message-ID: <3F1D9273.9030505@Lucent.com>
On 7/22/2003 11:21 AM, Charles Demas wrote:
>  In article <9cc83d20.0307220718.56ad7182@posting.google.com>,
>  Rob Eger <reger@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@dbforums.com>
Newsgroups: comp.unix.shell
Date: Tue, Jul 22, 2003 12:40 PM
Message-ID: <3139156.1058892012@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@hotmail.com (rakesh sharma)
Newsgroups: comp.unix.shell
Date: Tue, Jul 22, 2003 4:48 PM
Message-ID: <ed24e4cf.0307221248.16f109cd@posting.google.com>
reger@txcorp.com (Rob Eger) wrote in message 
news:<9cc83d20.0307220718.56ad7182@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@hotmail.com (rakesh sharma)
Newsgroups: comp.unix.shell
Date: Tue, Jul 22, 2003 10:20 PM
Message-ID: <ed24e4cf.0307221435.4e72d125@posting.google.com>
reger@txcorp.com (Rob Eger) wrote in message 
news:<9cc83d20.0307220718.56ad7182@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@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=380455


This archive was generated by hypermail 2.1.7 : Sat Apr 12 2008 - 10:49:41 EDT