linux - 从配置文件中删除 ip 地址

标签 linux bash pipe output cut

我有以下输出:

vif         = [ 'ip=1.2.3.4, mac=00:00:00:00:00:00, bridge=eth1', 'ip=5.6.7.8, mac=00:00:00:00:00:00, bridge=eth1' ]

有时候,只有一个ip地址。所以它是:

vif         = [ 'ip=1.2.3.4, mac=00:00:00:00:00:00, bridge=eth1' ]

而在其他情况下,有超过 2 个 ip 地址:

vif         = [ 'ip=1.2.3.4, mac=00:00:00:00:00:00, bridge=eth1', 'ip=5.6.7.8, mac=11:11:11:11:11:11, bridge=eth1', 'ip=9.1.2.3, mac=22:22:22:22:22:22, bridge=eth1' ]

有没有简单的方法只获取 ip 地址?我想将它们存储在一个数组中。

最佳答案

这是众多可能性中的一种:tr -s "[,'""\n"| grep "^ip="|剪切-d“=”-f2

例子:

echo "vif         = [ 'ip=1.2.3.4, mac=00:00:00:00:00:00, bridge=eth1', 'ip=5.6.7.8, mac=11:11:11:11:11:11, bridge=eth1', 'ip=9.1.2.3, mac=22:22:22:22:22:22, bridge=eth1' ]" | tr -s "[,'" "\n" | grep "^ip=" | cut -d "=" -f2

产生

1.2.3.4
5.6.7.8
9.1.2.3

关于linux - 从配置文件中删除 ip 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22217809/

相关文章:

linux - 每 10 秒清除我的脚本日志

bash - 获取每个用户名的计数,作为它们的最后一个修饰符的文件数量

linux - 尝试使用 bash 脚本制作 live/proc/reader 以进行实时过程监控

linux - shell 脚本: Excecute a command in "screen"

python - 如何将 python 脚本通过管道传递给命令 (boost::program::options)

检查管道状态

bash - 如何删除管道中的每个第二个字节?

linux - expect脚本中 `exit`命令后的数字是什么意思

ruby-on-rails - 删除 Rails 应用程序?

c - 如何使用 ptrace 跟踪程序执行?