[英]Modify Line in File Matching WORD1 and NOT WORD2 (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
. 我已经构建了这个awk
命令来在缓冲区中执行它,但是因为awk缺乏像sed
那样编辑的能力而无法将其放入文件本身。 (However I can't figure out how to do this type of match with sed
.) (但我无法弄清楚如何与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.) 这将查找匹配“kernel”和NOT “audit = 1”的行(必要时附加“audit = 1”。)
Tagged as sed/awk, but open to other suggestions. 标记为sed / awk,但对其他建议持开放态度。
使用sed:
sed -i '/kernel/{/audit=1/!s/$/ audit=1/}' /etc/grub.conf
The in-place editing functionality of sed
uses a temporary file that without a suffix, is used to replace the existing file. sed
的就地编辑功能使用没有后缀的临时文件来替换现有文件。 For example, this: 例如,这个:
sed -i '/\tkernel/ { /audit=1/!s/$/ audit=1/ }' /etc/grub.conf
is the same as this: 与此相同:
sed '/\tkernel/ { /audit=1/!s/$/ audit=1/ }' /etc/grub.conf > tmp && mv tmp /etc/grub.conf
The -i
flag just sugar coats the process. -i
标志只是糖涂层过程。 Therefore, you can simply apply the same logic to your awk
command: 因此,您可以简单地将相同的逻辑应用于awk
命令:
awk '/\tkernel/ && !/audit=1/ { print $0, "audit=1"; next }1' /etc/grub.conf > tmp && mv tmp /etc/grub.conf
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.