我正在学习awk
,我想知道是否有一个选项可以将更改写入文件,类似于sed
我将使用-i
选项保存对文件的修改.
我明白我可以使用重定向来编写更改.但是有没有选择awk
呢?
我正在尝试做一些应该简单且常规的事情。\n我想搜索文件 /etc/dhcpcd.conf 并更改分配给 eth0 的未注释的静态 IP 地址。示例测试文件如下所示:
\ninterface eth0\n# test variants of comments\n#static ip_address=192.168.21.40/24\n# static ip_address=192.168.21.40/24\n #static ip_address=192.168.21.40/24\n # static ip_address=192.168.21.40/24\n\n#the line to match and change\nstatic ip_address=123.223.122.005/24\n
Run Code Online (Sandbox Code Playgroud)\n我尝试在这里实现一个解决方案: \n这里\xe2\x80\x99s 是一种在 awk 中执行此操作的不那么神秘的方法,由 Scott 回答 - \xd0\xa1\xd0\xbb\xd0\xb0\xd0\xb2\ xd0\xb0\xd0\xa3\xd0\xba\xd1\x80\xd0\xb0\xd1\x97\xd0\xbd\xd1\x96
\n我选择 AWK 解决方案是因为它很容易阅读,但是我无法修改答案以适合我。我之前没有使用过sed
,awk
所以我写了一个测试脚本。
#!/bin/bash\n# testing on a copy of the config file\nconf_file="/home/user/dhcpcd.conf"\n# BASH variable with fake test ip\nnew_ip=111.222.123.234\n\ncat "$conf_file" | awk $\'\n /interface / { matched=0 }\n /interface eth0/ { matched=1 }\n matched …
Run Code Online (Sandbox Code Playgroud)