我正在整理一些 Bash 配置脚本来配置 Linux 服务器,我希望能够注释文件中所有未注释的行,但保留空白/空行完好无损。
使用the great answer to this question我能够想出这个 Sed one liner,效果很好:
sed -e '/^#/!s/\(.*\)/# \1/g' /etc/update-motd.d/90-updates-available
当然,当我在生产设置上运行此 Sed 命令时,-e
将与 -i
交换以就地写入文件,但无论如何,该文件成功被注释掉,如下所示:
#!/bin/sh
#
# if [ -x /usr/lib/update-notifier/update-motd-updates-available ]; then
# exec /usr/lib/update-notifier/update-motd-updates-available
# fi
但我理想地希望输出是这样的;请注意一个空白/空行是如何保持不变的。:
#!/bin/sh
# if [ -x /usr/lib/update-notifier/update-motd-updates-available ]; then
# exec /usr/lib/update-notifier/update-motd-updates-available
# fi
在 Sed 中有什么办法可以做到这一点吗?
最佳答案
这个怎么样:
sed -e 's/^\([^#].*\)/# \1/g' /etc/update-motd.d/90-updates-available
关于linux - 如何使用 Sed 仅注释掉未注释的行并保持空白/空行 100% 完好无损?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34982602/