BashScripts.org
http://bashscripts.org/forum/

need help w/ sed if word exists in file (sort of complicated
http://bashscripts.org/forum/viewtopic.php?f=21&t=1564
Page 1 of 1

Author:  dagummit [ Mon Mar 05, 2012 8:48 am ]
Post subject:  need help w/ sed if word exists in file (sort of complicated

hey...

I am in need of some help with finding words in my file on particular line, and if the word exists, it will skip appending the phrase and if the word does not exist, it will add it.

For example, here is the kernel line of grub.conf:
kernel /boot/vmlinuz-2.6.18-194.el5PAE ro root=LABEL=/ rhgb quiet

if this line doesn't have "audit=1", I need to add it. I can do this (by searching for vmlinuz-2.6)
Code:
sed -i '/vmlinuz-2.6/ s|$| audit=1|' /boot/grub/grub.conf


But some of my grub.conf files have multiple kernel lines with vmlinuz-2.6 and some already have the "audit=1" appended to them. So, I need to be able to search for those lines that do not have "audit=1" and append it, but ignore the lines that already have it.

Any ideas?

Author:  dagummit [ Mon Mar 05, 2012 11:34 am ]
Post subject:  Re: need help w/ sed if word exists in file (sort of complic

Just figured it out, in case anyone else finds a need for this:

Code:
sed -i '/vmlinuz-2.6/ {/audit=1/! s|$| audit=1|}' /boot/grub/grub.conf

Author:  jeo [ Fri Mar 09, 2012 11:52 am ]
Post subject:  Re: need help w/ sed if word exists in file (sort of complic

Nice, thanks for the update! I love a good sed one-liner :)

Page 1 of 1 All times are UTC - 6 hours
© 2000, 2002, 2005, 2007 phpBB Group • http://www.phpbb.com