Find the answer to your Linux question:
Results 1 to 2 of 2
Hi, Easy question hopefully. I have a file with the contents foo foo bar bar bar foo foo And I want to add a line 'a pointless line' after the ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2009
    Posts
    4

    sed question


    Hi,

    Easy question hopefully. I have a file with the contents

    foo
    foo

    bar
    bar
    bar

    foo
    foo

    And I want to add a line 'a pointless line' after the last line containing the string bar. How would I do this with sed ?

    I managed :

    sed ' /bar/ a\
    a pointless line' < ./foobar.txt

    which prints :

    foo
    foo

    bar
    a pointless line
    bar
    a pointless line
    bar
    a pointless line

    foo
    foo


    So not quite the desired effect as I only wanted it to print:

    foo
    foo

    bar
    bar
    bar
    a pointless line

    foo
    foo

  2. #2
    Linux Newbie
    Join Date
    Jan 2008
    Location
    Canada
    Posts
    109
    Hi ed91270
    Hi. It would appear to me that if you want to parse a file and enter a line, as you mention after the last instance of <bar>, you must parse the file twice. Once to locate the last line where the <bar> occurs, possible by line number, and second pass insert your <line> at Line Number + one. Not sure how else you could know where the last instance of <bar> is located. Not sure this helps much. Cheers...
    Robert

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •