【问题标题】:Pipe grep search result into sed replace管道 grep 搜索结果到 sed 替换
【发布时间】:2014-05-19 11:18:43
【问题描述】:

我正在编写一个脚本来使用当前动态地址更新主机文件。我从 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} 中?是否可以在一行中执行此操作?

【问题讨论】:

    标签: bash sed grep


    【解决方案1】:

    使用command substitution:

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

    【讨论】:

      猜你喜欢
      • 2014-12-17
      • 1970-01-01
      • 2010-11-13
      • 2011-07-29
      • 2014-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-18
      相关资源
      最近更新 更多