【发布时间】:2020-11-14 02:20:03
【问题描述】:
我想用 sed 和正则表达式匹配 stdout 命令中的字符串
例如我有这个输出命令: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 = [ ]
我想从Interface=eth0,(第8行)获取eth0。
所以我使用这个命令: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?
谢谢。
【问题讨论】:
标签: regex linux sed match comma