[英]Running "ip | grep | awk" within a sed replacement
Problem Set (Raspberry Pi OS): I have a file " example.conf " that contains a line IPv4addr=XXXXX .问题集(Raspberry Pi OS):我有一个文件“ example.conf ”,其中包含一行IPv4addr=XXXXX 。 I am attempting to change this to the IP that is generated the in the command ipTest=$(ip --brief a show | grep eth0 | awk '{ print $3 }')我正在尝试将其更改为 IP 生成的命令ipTest=$(ip --brief a show | grep eth0 | Z5E4C8DFA9E20567E2655E847F681998) { $3
I want to automate this file change during a script "install.sh", the line I am attempting is:我想在脚本“install.sh”期间自动执行此文件更改,我正在尝试的行是:
IPtest=$(ip --brief a show | grep eth0 | awk '{ print $3 }') IPtest=$(ip --brief a show | grep eth0 | awk '{ print $3 }')
sudo sed -e "/IPv4addr/s/[^=]*$/$IPtest/" example.conf须藤 sed -e "/IPv4addr/s/[^=]*$/$IPtest/" example.conf
Returns error: sed: -e expression #1, char 32: unknown option to `s'返回错误: sed: -e expression #1, char 32: unknown option to `s'
A simple line in that code works, such as SimpleTest='Works'该代码中的简单行有效,例如SimpleTest='Works'
Any thoughts?有什么想法吗? I am open to other solutions as well, however I am not an experienced linux user so I am using the tools I know to work with other problem sets.我也对其他解决方案持开放态度,但是我不是经验丰富的 linux 用户,所以我使用我知道的工具来处理其他问题集。
You can shorten your variable and allow awk
to do the job of grep
at the same time您可以缩短变量并允许awk
同时完成grep
的工作
IPtest=$(ip --brief a s | awk '/eth0/{print $3}')
Using sed
grouping and back referencing使用sed
分组和反向引用
sed -i.bak "s|\([^=]*.\).*|\1$IPtest|" example.conf
$IPtest contains "/" character, try something like that $IPtest 包含“/”字符,试试这样
IPtest=$(ip --brief a show | grep eth0 | awk '{ print $3 }')
sudo sed -e '/IPv4addr/s@[^=]*$@'"$IPtest"'@' example.conf
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.