Find the answer to your Linux question:
Results 1 to 3 of 3
Hey all, I've seen plenty of places that talked about adding lines w/sed after finding a pattern. However, I haven't seen how to append immediately after a pattern without just ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Feb 2007
    Posts
    6

    Question SED - Append immediately after pattern


    Hey all,

    I've seen plenty of places that talked about adding lines w/sed after finding a pattern. However, I haven't seen how to append immediately after a pattern without just substituting the whole line.

    i.e.

    destination d_auth { file("/var/log/secure"); };

    appending udp(x.x.x.x port(xxx) localport(xxx));

    after

    /var/log/secure");

    to make

    destination d_auth { file("/var/log/secure"); udp(x.x.x.x port(xxx) localport(xxx)); };

    Any ideas?

    Thanks,

    Chris

  2. #2
    Linux Enthusiast
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    661
    for example the contents of the file, /tmp/junk, is
    Code:
    blah blah
    blah blah
    fred blah blah
    blah blah
    blah blah
    and you wish to add the lines of:
    Code:
    newline one
    newline two
    after the line containing the pattern "fred", the the sed script fiel would be:
    Code:
    /fred/ a\
    newline one\
    newline two
    A sample execution below:
    Code:
    bash$ sed -f /tmp/sedscript  /tmp/junk 
    blah blah
    blah blah
    fred blah blah
    newline one
    newline two
    blah blah
    blah blah
    bash$

  3. #3
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,293
    Hi.

    If you choose to use sed, there are 2 possible pitfalls, both with the characters in your data. The first is the "/" in the pathname of the data, the second is the double quote in the inserted string. Here's one solution:
    Code:
    #!/usr/bin/env bash
    
    # @(#) s2	Demonstrate new string insertion after unique string.
    
    # Utility functions: print-as-echo, print-line-with-visual-space, debug.
    # export PATH="/usr/local/bin:/usr/bin:/bin"
    pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
    pl() { pe;pe "-----" ;pe "$*"; }
    db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
    db() { : ; }
    C=$HOME/bin/context && [ -f $C ] && $C sed
    
    FILE=${1-data1}
    
    pl " Input data file $FILE:"
    cat $FILE
    new='udp(x.x.x.x port(xxx) localport(xxx));'
    unique='/var/log/secure");'
    
    pe
    pe " Adding [$new]"
    pe " after [$unique]"
    
    pl " Results:"
    sed -e "s|^\(.*$unique\)\(.*\)$|\1$new\2|" $FILE
    
    echo
    echo " Expected output:"
    echo 'destination d_auth { file("/var/log/secure"); udp(x.x.x.x port(xxx) localport(xxx)); };'
    
    exit 0
    producing:
    Code:
    % ./s2
    
    Environment: LC_ALL = C, LANG = C
    (Versions displayed with local utility "version")
    OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
    Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
    GNU bash 3.2.39
    GNU sed version 4.1.5
    
    -----
     Input data file data1:
    destination d_auth { file("/var/log/secure"); };
    
     Adding [ udp(x.x.x.x port(xxx) localport(xxx));]
     after [/var/log/secure");]
    
    -----
     Results:
    destination d_auth { file("/var/log/secure"); udp(x.x.x.x port(xxx) localport(xxx)); };
    
     Expected output:
    destination d_auth { file("/var/log/secure"); udp(x.x.x.x port(xxx) localport(xxx)); };
    See man sed or sed tutorials for details.

    Note that data and code posted in threads are more easily readable if they are placed in CODE segments. Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  4. $spacer_open
    $spacer_close

Posting Permissions

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