Register
It is currently Thu Nov 27, 2014 5:35 am

need help w/ sed if word exists in file (sort of complicated


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Mon Mar 05, 2012 8:48 am   

Joined: Tue Apr 19, 2011 11:01 pm
Posts: 36
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?


Top
 Profile  
 PostPosted: Mon Mar 05, 2012 11:34 am   

Joined: Tue Apr 19, 2011 11:01 pm
Posts: 36
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


Top
 Profile  
 PostPosted: Fri Mar 09, 2012 11:52 am   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Nice, thanks for the update! I love a good sed one-liner :)


Top
 Profile YIM  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP