我想将 stdout 命令中的字符串与 sed 和 regex 匹配
例如我有这个输出命令: connmanctl services ethernet_00142d000000_cable
这个输出:
Type = ethernet
Security = [ ]
State = ready
Favorite = True
Immutable = False
AutoConnect = True
Name = Wired
Ethernet = [ Method=auto, Interface=eth0, Address=00:00:00:00:00:00, MTU=1500 ]
IPv4 = [ Method=manual, Address=192.168.0.100, Netmask=255.255.255.0 ]
IPv4.Configuration = [ Method=manual, Address=192.168.0.101, Netmask=255.255.255.0 ]
IPv6 = [ ]
IPv6.Configuration = [ Method=auto, Privacy=disabled ]
Nameservers = [ ]
Nameservers.Configuration = [ ]
Timeservers = [ ]
Timeservers.Configuration = [ ]
Domains = [ ]
Domains.Configuration = [ ]
Proxy = [ Method=direct ]
Proxy.Configuration = [ ]
Provider = [ ]
我想eth0
从Interface=eth0,
(第 8 行)获取。
所以我使用这个命令: services ethernet_00142d000000_cable | sed -n -e 's/^.*Ethernet = //p' | sed -e 's/.*Interface=\([^,*]*\),*/\1/'
第一个 sed 使用以太网提取整行,第二个 sed 提取以 Interface 开头并以逗号结尾的字符串。
结果是:
eth0 Address=00:14:2D:00:00:00, MTU=1500 ]
为什么我在逗号后已经得到了以下内容。我怎么才能得到eth0
?
谢谢你。
以下是如何使用单个 sed 命令将您的编辑定位到特定行:
services ethernet_00142d000000_cable | \
sed -n -e '/Ethernet =/s/.*Interface=\([^,]*\).*/\1/p'
得到它?您可以s///
通过在命令之前添加模式(甚至是行范围规范)来限制该命令。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句