gpt4 book ai didi

bash - 管道 grep 搜索结果到 sed 替换

转载 作者:行者123 更新时间:2023-11-29 09:47:52 26 4
gpt4 key购买 nike

我正在编写脚本以使用当前动态地址更新主机文件。我从 ifconfig 获取地址并使用 sed 更新/etc/hosts。

#following returns the current VPN address e.g. 10.8.0.14
ifconfig | grep -oP 'inet addr:\K(.*)?(?= P-t-P)'

#following can be used to replace the original one with the VPN address
sudo sed -i 's/127.0.1.1/{new address}/g' /etc/hosts

但是如何将 grep 的输出通过管道传输到 sed 中的替换文本 {new address} 中?是否可以在一条线上完成此操作?

最佳答案

使用command substitution :

sudo sed -i "s/127.0.1.1/$(ifconfig | grep -oP 'inet addr:\K(.*)?(?=  P-t-P)')/g" /etc/hosts

关于bash - 管道 grep 搜索结果到 sed 替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23729211/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com