1753325 Members
5507 Online
108792 Solutions
New Discussion юеВ

Re: sed question

 
SOLVED
Go to solution
Allanm
Super Advisor

sed question

Hi!

I want to add line y after line x in httpd.conf file but struggling with it...

line x #CustomLog logs/localhost/access_log combined

line y CustomLog "|/bin/cat - >> /apache/logs/localhost/access_log" combined

Can you suggest a sed one liner for this. Mine doesnt work and gives wierd behaviour -

sed '=#CustomLog logs\/localhost\/access_log combined= a\CustomLog \"\|\/bin\/cat \- \>\> \/apache\/logs\/localhost\/access_log\" combined' httpd.conf >> httpd.conf.2

Thanks,
Allanm
6 REPLIES 6
Allanm
Super Advisor

Re: sed question

Please Help!
Peter Nikitka
Honored Contributor

Re: sed question

Hi,

if this is a one-timer, use vi.
If this has to be done in a script:

...
{
print "/CustomLog logs\/localhost\/access_log combined/a"
print 'CustomLog "|/bin/cat - >> /apache/logs/localhost/access_log" combined'
print '.\nw'
} | ed - http.conf
...

mfG Peter

PS: untested
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Bob E Campbell
Honored Contributor

Re: sed question

You have an interesting set of strings that are going to call for some escapes, but in general this would be done in sed as a straight append command:

sed -e '/^TargetString/a\
Appended string here'

Your string has so many quotes and slashes that it will take me a minute. If you do not need to match on the whole string that would certainly help.
Bob E Campbell
Honored Contributor
Solution

Re: sed question

sed -e '/^#CustomLog logs\/localhost\/access_log combined/ a\
CustomLog "|/bin/cat - >> /apache/logs/localhost/access_log combined"
'
James R. Ferguson
Acclaimed Contributor

Re: sed question

Hi:

You could do:

# perl -ne 'if (/^#CustomLog/) {print;print qq(INSERT_ > "SOME" < _LINE\n)} else {print}' file

...Notice that the line to be inserted actually looks like:

INSERT_ > "SOME" < _LINE

...that is, you are free to put any string you want in its place, thus, you want:

# perl -ne 'if (/^#CustomLog/) {print;print qq(CustomLog "|/bin/cat - >> /apache/logs/localhost/access_log" combined\n)} else {print}' file

Regards!

...JRF...
Allanm
Super Advisor

Re: sed question

Thanks All!!!