SUMMARY: inserting a blank line in a file

From: Rich Glazier (RichGlazier_at_netscape.net)
Date: 10/29/03

  • Next message: Rich Glazier: "SUMMARY: getting info on print queues with queued print jobs."
    Date: Wed, 29 Oct 2003 13:24:55 -0500
    To: tru64-unix-managers@ornl.gov
    
    

    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
    

  • Next message: Rich Glazier: "SUMMARY: getting info on print queues with queued print jobs."

    Relevant Pages

    • Re: Help required in VS.Net Crystal Report
      ... Look for newsgroups with either the ... > I'm new to VS.Net Crystal Report. ... > Ptid String ... > Prid String ...
      (microsoft.public.vb.general.discussion)
    • Re: return() value
      ... programs of simultaneous execution. ... string, are completely implementation-defined. ... the other program opens the file and reads that value. ... have an appropriate newsgroups line in your header for your mail to be seen, ...
      (comp.lang.c.moderated)
    • Re: Test
      ... "other" discussions that frequent these groups; especially the foul language ... when I first found out about newsgroups and then I lurked for months before ... > My contention is doing a test is a lesser violation than some of the ... a string of messages not related to Windows of any version. ...
      (microsoft.public.windowsxp.general)
    • Re: ok characters
      ... > newsgroups devoted exclusively to .NET programming. ... >> however I want to define the string of characters that AREN'T ok ... does the _tcscspn function have ...
      (microsoft.public.vb.general.discussion)
    • Re: Opening files according to extension
      ... "Carlos Chalhoub" wrote in message ... >> Please post any further questions or followup to the newsgroups for the ... >>> aTitle As String, aSubject As String, aAuthor As String, aCompany As ...
      (microsoft.public.word.vba.beginners)