how to search a pattern and append something in txt file? - Unix

This is a discussion on how to search a pattern and append something in txt file? - Unix ; Hi, Could somebody tell me ho to use sed or awk to append two lines after each occurence of line which begins with defined word? i.e. if there is a line in the txt file which begins with pattern "dn", ...

+ Reply to Thread
Results 1 to 6 of 6

Thread: how to search a pattern and append something in txt file?

  1. how to search a pattern and append something in txt file?

    Hi,

    Could somebody tell me ho to use sed or awk to append two lines after each
    occurence of line which begins with defined word?

    i.e.
    if there is a line in the txt file which begins with pattern "dn", the next
    two lines should be appended i.e line1 and line2.

    thanks in advance



  2. Re: how to search a pattern and append something in txt file?


    Użytkownik "darek.wj" napisał w wiadomości
    news:gcjcp3$8h0$1@news.man.bydgoszcz.pl...
    > Hi,
    >
    > Could somebody tell me ho to use sed or awk to append two lines after each
    > occurence of line which begins with defined word?
    >
    > i.e.
    > if there is a line in the txt file which begins with pattern "dn", the
    > next two lines should be appended i.e line1 and line2.
    >
    > thanks in advance
    >

    I tried to use
    sed /^dn/a\line1line2 file.txt
    but I dont know how to put line2 in the separated, next line (after the
    line1) in the file.

    The second problem is that I don't kno how to handle these lines when they
    contain spaces.

    I would appreciate any help

    thanks



  3. Re: how to search a pattern and append something in txt file?

    "darek.wj" writes:
    >
    >Użytkownik "darek.wj" napisał w wiadomości
    >news:gcjcp3$8h0$1@news.man.bydgoszcz.pl...
    >> Hi,
    >>
    >> Could somebody tell me ho to use sed or awk to append two lines after each
    >> occurence of line which begins with defined word?
    >>
    >> i.e.
    >> if there is a line in the txt file which begins with pattern "dn", the
    >> next two lines should be appended i.e line1 and line2.
    >>
    >> thanks in advance
    >>

    >I tried to use
    >sed /^dn/a\line1line2 file.txt
    >but I dont know how to put line2 in the separated, next line (after the
    >line1) in the file.
    >
    >The second problem is that I don't kno how to handle these lines when they
    >contain spaces.
    >
    >I would appreciate any help
    >
    >thanks

    $ cat /tmp/ccc
    fawer
    fasdf
    fred
    joe
    sam
    scott

    $ cat /tmp/ccc | sed -e "/^fred/aa\nb"
    fawer
    fasdf
    fred
    a
    b
    joe
    sam
    scott



  4. Re: how to search a pattern and append something in txt file?

    On Oct 8, 4:12 pm, sc...@slp53.sl.home (Scott Lurndal) wrote:
    ....
    > >I tried to use
    > >sed /^dn/a\line1line2 file.txt
    > >but I dont know how to put line2 in the separated, next line
    > >(after the line1) in the file.

    ....
    > $ cat /tmp/ccc
    > fawer
    > fasdf
    > fred
    > joe
    > sam
    > scott
    >
    > $ cat /tmp/ccc | sed -e "/^fred/aa\nb"
    > fawer
    > fasdf
    > fred
    > a
    > b
    > joe
    > sam
    > scott


    The use of \n in the sed expression is neither standardized nor
    portable. The portable form is:

    cat /tmp/ccc | sed -e '/^fred/a\
    a\
    b
    '

    Note the use of single-quotes instead of double-quotes there. If you
    use double-quotes then double the backslashes:

    cat /tmp/ccc | sed -e "/^fred/a\\
    a\\
    b
    "

    This assumes you're using a bourne-style shell and not csh. For csh,
    you have to stuff the sed bits into a file and use sed -f with that,
    as csh doesn't permit newlines in command arguments.


    > >The second problem is that I don't kno how to handle these lines
    > > when they contain spaces.


    What's the problem? You can include spaces in the text:

    cat /tmp/ccc | sed -e '/^fred/a\
    a a\
    bb bb
    '

    Philip Guenther

  5. Re: how to search a pattern and append something in txt file?

    > $ cat /tmp/ccc | sed -e "/^fred/aa\nb"

    Thanks a lot

  6. Re: how to search a pattern and append something in txt file?

    On Thursday 9 October 2008 00:45, darek.wj wrote:

    > Hi,
    >
    > Could somebody tell me ho to use sed or awk to append two lines after each
    > occurence of line which begins with defined word?
    >
    > i.e.
    > if there is a line in the txt file which begins with pattern "dn", the
    > next two lines should be appended i.e line1 and line2.
    >
    > thanks in advance


    awk '1;/^dn/{print "line1\nline2"}' file

    sed '/^dn/{a\
    line1
    a\
    line2
    }' file


+ Reply to Thread