在匹配的单词后追加新行,但忽略如果这些行已存在。

huangapple go评论60阅读模式
英文:

sed + append new lines after matched word but ignore if lines are exists

问题

我们创建以下sed命令行以在匹配的单词之后添加行(在Red Hat 7.x机器上):

sed -i '/\[Service\]/a MemoryAccounting=yes\nMemoryCurrent=8192000\nMemoryLimit=8192000' /lib/systemd/system/rsyslog.service

在更新之前的文件示例:

cat /lib/systemd/system/rsyslog.service
[Unit]
Description=System Logging Service
;Requires=syslog.socket
Wants=network.target network-online.target
After=network.target network-online.target
Documentation=man:rsyslogd(8)
Documentation=http://www.rsyslog.com/doc/

[Service]
Type=notify
EnvironmentFile=-/etc/sysconfig/rsyslog
ExecStart=/usr/sbin/rsyslogd -n $SYSLOGD_OPTIONS
Restart=on-failure
UMask=0066
StandardOutput=null
Restart=on-failure

[Install]
WantedBy=multi-user.target
;Alias=syslog.service

通过sed进行更新后的文件示例:

cat /lib/systemd/system/rsyslog.service
[Unit]
Description=System Logging Service
;Requires=syslog.socket
Wants=network.target network-online.target
After=network.target network-online.target
Documentation=man:rsyslogd(8)
Documentation=http://www.rsyslog.com/doc/

[Service]
MemoryAccounting=yes
MemoryCurrent=8192000
MemoryLimit=8192000
Type=notify
EnvironmentFile=-/etc/sysconfig/rsyslog
ExecStart=/usr/sbin/rsyslogd -n $SYSLOGD_OPTIONS
Restart=on-failure
UMask=0066
StandardOutput=null
Restart=on-failure

[Install]
WantedBy=multi-user.target
;Alias=syslog.service

现在的问题是,当再次运行sed命令时,我们会得到重复的行:

[Service]
MemoryAccounting=yes
MemoryCurrent=8192000
MemoryLimit=8192000
MemoryAccounting=yes
MemoryCurrent=8192000
MemoryLimit=8192000

有没有建议如何在行已经存在时忽略编辑?

英文:

we create the follwinmg sed line in order to add the lines after mached word ( on redhat 7.x machines )

sed -i '/\[Service\]/a MemoryAccounting=yes\nMemoryCurrent=8192000\nMemoryLimit=8192000' /lib/systemd/system/rsyslog.service

example of the file before update

cat /lib/systemd/system/rsyslog.service
[Unit]
Description=System Logging Service
;Requires=syslog.socket
Wants=network.target network-online.target
After=network.target network-online.target
Documentation=man:rsyslogd(8)
Documentation=http://www.rsyslog.com/doc/

[Service]
Type=notify
EnvironmentFile=-/etc/sysconfig/rsyslog
ExecStart=/usr/sbin/rsyslogd -n $SYSLOGD_OPTIONS
Restart=on-failure
UMask=0066
StandardOutput=null
Restart=on-failure

[Install]
WantedBy=multi-user.target
;Alias=syslog.service

example of the file after the update by sed

cat /lib/systemd/system/rsyslog.service
[Unit]
Description=System Logging Service
;Requires=syslog.socket
Wants=network.target network-online.target
After=network.target network-online.target
Documentation=man:rsyslogd(8)
Documentation=http://www.rsyslog.com/doc/

[Service]
MemoryAccounting=yes
MemoryCurrent=8192000
MemoryLimit=8192000
Type=notify
EnvironmentFile=-/etc/sysconfig/rsyslog
ExecStart=/usr/sbin/rsyslogd -n $SYSLOGD_OPTIONS
Restart=on-failure
UMask=0066
StandardOutput=null
Restart=on-failure

[Install]
WantedBy=multi-user.target
;Alias=syslog.service

now the problem is when we run again the sed line then we get duplicate lines as

[Service]
MemoryAccounting=yes
MemoryCurrent=8192000
MemoryLimit=8192000
MemoryAccounting=yes
MemoryCurrent=8192000
MemoryLimit=8192000

any suggestion how to ignore editing when the lines are already exists ?

notes:

in order to complete service update we need to do the following:

systemctl daemon-reload
systemctl restart rsyslog.service

答案1

得分: 1

这部分的中文翻译如下:

这应该可以工作

sed -i.SAVED '/[Service]/N;
s/\n/ /;
/[Service] Type=notify/c
[Service]\
MemoryAccounting=yes\
MemoryCurrent=8192000\
MemoryLimit=8192000\
Type=notify

/[Service] M.*/s/ /\n/
' /lib/systemd/system/rsyslog.service


它使用 `N` 连接行,将 `nl` 替换为空格,检查是否与原始行匹配。

同时保存原始文件以防万一。
英文:

This should work

sed -i.SAVED '/\[Service\]/N;
s/\n/ /;
/\[Service\] Type=notify/c\
[Service]\
MemoryAccounting=yes\
MemoryCurrent=8192000\
MemoryLimit=8192000\
Type=notify

/\[Service\] M.*/s/ /\n/
' /lib/systemd/system/rsyslog.service

it's joining lines using N, replacing the nl with space checking if it matches the original line.

Also saving the original file just in case.

答案2

得分: 1

使用sed命令

$ sed -Eei.bak '/\[service]/I{n;/^memoryaccounting/I!{i MemoryAccounting=yes\nMemoryCurrent=8192000\nMemoryLimit=8192000' -e '}}' input_file
[Unit]
Description=System Logging Service
;Requires=syslog.socket
Wants=network.target network-online.target
After=network.target network-online.target
Documentation=man:rsyslogd(8)
Documentation=http://www.rsyslog.com/doc/

[Service]
MemoryAccounting=yes
MemoryCurrent=8192000
MemoryLimit=8192000
Type=notify
EnvironmentFile=-/etc/sysconfig/rsyslog
ExecStart=/usr/sbin/rsyslogd -n $SYSLOGD_OPTIONS
Restart=on-failure
UMask=0066
StandardOutput=null
Restart=on-failure

[Install]
WantedBy=multi-user.target
;Alias=syslog.service
英文:

Using sed

$ sed -Eei.bak '/\[service]/I{n;/^memoryaccounting/I!{i MemoryAccounting=yes\nMemoryCurrent=8192000\nMemoryLimit=8192000' -e '}}' input_file
[Unit]
Description=System Logging Service
;Requires=syslog.socket
Wants=network.target network-online.target
After=network.target network-online.target
Documentation=man:rsyslogd(8)
Documentation=http://www.rsyslog.com/doc/

[Service]
MemoryAccounting=yes
MemoryCurrent=8192000
MemoryLimit=8192000
Type=notify
EnvironmentFile=-/etc/sysconfig/rsyslog
ExecStart=/usr/sbin/rsyslogd -n $SYSLOGD_OPTIONS
Restart=on-failure
UMask=0066
StandardOutput=null
Restart=on-failure

[Install]
WantedBy=multi-user.target
;Alias=syslog.service

答案3

得分: 1

这个 sed 命令会在 [Service] 后面紧跟着以 MemoryAccounting= 开头的行之外跳过文本插入。

sed -i.orig '
/\[Service]/{
n
/^MemoryAccounting=/b
i\
MemoryAccounting=yes\
MemoryCurrent=8192000\
MemoryLimit=8192000
}' rsyslog.service
英文:

This sed command will skip the insertion of text if the [Service] is immediately followed by a line starting with MemoryAccounting=.

sed -i.orig '
/\[Service]/{
n
/^MemoryAccounting=/b
i\
MemoryAccounting=yes\
MemoryCurrent=8192000\
MemoryLimit=8192000
}' rsyslog.service

huangapple
  • 本文由 发表于 2023年1月6日 13:21:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75027207.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定