Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 在文件中查找子字符串并注释该行,然后在注释行下方插入新行_Bash_Unix_Awk_Sed_Grep - Fatal编程技术网

Bash 在文件中查找子字符串并注释该行,然后在注释行下方插入新行

Bash 在文件中查找子字符串并注释该行,然后在注释行下方插入新行,bash,unix,awk,sed,grep,Bash,Unix,Awk,Sed,Grep,我试图在文件中查找子字符串——端口1234,如果该行没有注释,则用#注释掉该行,并在其下方插入一行定义为这是新路径:/new/path/to/file。如果包含--port 1234的行已被注释,则不执行任何操作。如果文件中未找到子字符串--port 1234,则回显“not found” 样本输入: somecode somecode somecode somecode --port 1234 somecode somecode somecode somecode somecode 样本输出

我试图在文件中查找子字符串
——端口1234
,如果该行没有注释,则用
#
注释掉该行,并在其下方插入一行定义为
这是新路径:/new/path/to/file
。如果包含
--port 1234
的行已被注释,则不执行任何操作。如果文件中未找到子字符串--port 1234,则
回显“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;

到目前为止,我只知道如何忽略该行是否已注释,或者该行是否不包含
--端口1234
。非常新的bash脚本

awk
更适合这份工作

示例文件:

cat文件
富吧
#somecode somecode——端口1234 somecode somecode somecode
somecode somecode
somecode somecode——端口1234 somecode somecode somecode
somecode somecode
使用
gnu awk
作为:

awk-i in place'/--端口1234/&&!/^#/{
打印“#”$0或“这是新路径:/new/path/to/file”
下一个
}1'文件
富吧
#somecode somecode——端口1234 somecode somecode somecode
somecode somecode
#somecode somecode——端口1234 somecode somecode somecode
这是新路径:/new/path/to/file
somecode somecode

坚持
sed
理念:

$ sed -E 's|^([^#].*--port 1234.*)$|#\1\nThis the new path: /new/path/to/file|' myfile
somecode somecode
#somecode somecode --port 1234 somecode somecode somecode
This the new path: /new/path/to/file
somecode somecode
#somecode somecode --port 1234 somecode somecode somecode
somecode somecode
示例输入,带和不带前导注释(
#
):

一个
sed
想法:

$ sed -E 's|^([^#].*--port 1234.*)$|#\1\nThis the new path: /new/path/to/file|' myfile
somecode somecode
#somecode somecode --port 1234 somecode somecode somecode
This the new path: /new/path/to/file
somecode somecode
#somecode somecode --port 1234 somecode somecode somecode
somecode somecode

一旦OP对结果感到满意,就可以添加
-i
标志来执行文件的就地更新。

它只是在终端中打印输出(尽管正确),但文件没有更改。我希望该文件可以自己编辑!你的awk版本是什么?用
awk-V
3.1.7检查我的GNU awkin位置是否与3.1.7不兼容。请尝试使用
sed
grep
好吗?然后使用:
awk'/--port 1234/&&!/^/{print“#“$0 ORS”这是新路径:/new/path/to/file;next}1'file>file.tmp&&mv file.tmp file
这是一个很好的解决方案,但是如果我第二次发出
sed
命令,
这将再次打印新路径:/new/path/to/file
。我不想那样。你能通过grep或if条件在你的解决方案中解决这个问题吗?我误读了之前被注释掉的那部分;答案更新你总是问同样的问题。你自己试试,当你不能解决你的问题的时候来这里。