【发布时间】:2021-07-26 02:30:31
【问题描述】:
我正在尝试在文件中查找子字符串--port 1234,如果该行没有被注释,则用# 注释掉该行,并在其下方插入一个定义为this is the new path: /new/path/to/file 的新行。如果包含--port 1234 的行已被注释,则什么也不做。如果在文件中找不到子字符串--port 1234,则echo "not found"
示例输入:
somecode somecode
somecode somecode --port 1234 somecode somecode somecode
somecode somecode
样本输出:
somecode somecode
#somecode somecode --port 1234 somecode somecode somecode
This is the new path: /new/path/to/file
somecode somecode
这是我目前所拥有的:
sed -E '/--port 1234/!b;/^[^#]/!b;
到目前为止,我只知道如果该行已经被注释,或者如果一行不包含--port 1234,如何忽略它。对 bash 脚本非常陌生!
【问题讨论】: