Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify Line in File Matching WORD1 and NOT WORD2 (Sed/Awk)

Tags:

regex

sed

awk

I need to add kernel parameters to lines not already containing them. (Don't want to add it to all lines in case it's already there.)

I've built this awk command to do it in buffer, but having trouble getting it into the file itself since awk lacks the ability to edit in place like sed. (However I can't figure out how to do this type of match with sed.)

awk '/\tkernel/&&!/audit=1/ { print $0" audit=1"; }' /etc/grub.conf

This looks for lines matching "kernel" and NOT "audit=1" (appending " audit=1" as necessary.)

Tagged as sed/awk, but open to other suggestions.

like image 363
Aaron Copley Avatar asked Jan 29 '26 20:01

Aaron Copley


1 Answers

Using sed:

sed -i '/kernel/{/audit=1/!s/$/ audit=1/}' /etc/grub.conf
like image 118
perreal Avatar answered Feb 01 '26 16:02

perreal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!