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.
- 09-16-2011 #1Just Joined!
- Join Date
- Feb 2007
- Posts
- 6
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
- 09-17-2011 #2Linux User
- Join Date
- Jan 2005
- Location
- Saint Paul, MN
- Posts
- 394
for example the contents of the file, /tmp/junk, is
and you wish to add the lines of:Code:blah blah blah blah fred blah blah blah blah blah blah
after the line containing the pattern "fred", the the sed script fiel would be:Code:newline one newline two
A sample execution below:Code:/fred/ a\ newline one\ newline two
Code:bash$ sed -f /tmp/sedscript /tmp/junk blah blah blah blah fred blah blah newline one newline two blah blah blah blah bash$
- 09-17-2011 #3Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,199
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:
producing: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
See man sed or sed tutorials for details.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)); };
Note that data and code posted in threads are more easily readable if they are placed in CODE segments. Best wishes ... cheers, drlWelcome - 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 )


Reply With Quote
