1

文本文件:

auto lo eth0 eth0.2 eth0.3
iface lo inet loopback
iface eth0 inet dhcp
iface eth0.2 inet static
      address 192.168.67.1
      network 192.168.67.0
      netmask 255.255.255.0
      broadcast 192.168.67.255
iface eth0.3 inet static
      address 192.168.68.1
      network 192.168.68.0
      netmask 255.255.255.0
      broadcast 192.168.68.255

Extarct only eth0 word only not eth0.2 or eth0.3 Desired:eth0 word, its line number

4

6 回答 6

2

如果您的grep版本支持该-P标志:

grep -noP "(?<=^| )eth0(?=$| )" file.txt

结果:

1:eth0
3:eth0
于 2012-10-31T06:13:49.387 回答
1
$> ... | grep -P "eth0[^\.]" 
auto lo eth0 eth0.2 eth0.3
iface eth0 inet dhcp

顺便说一句,“仅提取 eth0 单词”是什么意思?有 -o标志,例如:

$> ... | grep -o -P "eth0[^\.]"
eth0 
eth0
于 2012-10-31T06:15:21.657 回答
1
$ awk -v iface="eth0" '$0 ~ "(^| )"iface"( |$)" { print NR, iface }' file
1 eth0
3 eth0
于 2012-10-31T14:17:02.150 回答
1

如果我正确理解您的要求:

awk 'NR>1 && / eth0 /{print "eth0", NR}' file
于 2012-10-31T06:10:00.330 回答
1

我猜 op 想要匹配非别名、非回溯界面,例如:

awk '$1 == "iface" && $2 ~ /eth[0-9]+$/ { print $2}'

甚至:

awk -vFS='[ .]' '$1 == "iface" && $2 ~ /^eth/ { print $2; exit}'
于 2012-10-31T06:44:13.387 回答
1

Extarct only eth0 word only not eth0.2 or eth0.3 Desired:eth0 word, its line number

用 grep 来做:

grep -on 'iface eth0 ' file

输出:

3:iface eth0 

追加| sed 's/iface //'删除iface

于 2012-10-31T08:49:37.980 回答