Given this specific line pulled from ifconfig
, in my case:
考虑到ifconfig中提取的这一行,在我的例子中:
inet 192.168.2.13 netmask 0xffffff00 broadcast 192.168.2.255
inet 192.168.2.13 netmask 0xffffffff00广播192.168.2.255
How could one extract the 192.168.2.13
part (the local IP address), presumably with regex?
怎么可能一个提取192.168.2.13部分(本地IP地址),大概与正则表达式?
9 个解决方案
#1
48
Here's one way using grep
:
这里有一个使用grep的方法:
line='inet 192.168.2.13 netmask 0xffffff00 broadcast 192.168.2.256'
echo "$line" | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b"
Results:
结果:
192.168.2.13
192.168.2.256
If you wish to select only valid addresses, you can use:
如果你想只选择有效的地址,您可以使用:
line='inet 192.168.0.255 netmask 0xffffff00 broadcast 192.168.2.256'
echo "$line" | grep -oE "\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"
Results:
结果:
192.168.0.255
Otherwise, just select the fields you want using awk
, for example:
否则,选择你想要的字段使用awk,例如:
line='inet 192.168.0.255 netmask 0xffffff00 broadcast 192.168.2.256'
echo "$line" | awk -v OFS="\n" '{ print $2, $NF }'
Results:
结果:
192.168.0.255
192.168.2.256
Addendum:
附录:
Word boundaries: \b
单词边界:\ b
#2
3
use this regex ((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(?=\s*netmask)
使用这个正则表达式((25[0 - 5]| 2[0 - 9][0 - 4]|(01)?[0 - 9][0 - 9]?)\){ 3 }(25(0 - 5)| 2[0 - 9][0 - 4]|(01)?[0 - 9][0 - 9]?)(? = \ s *子网掩码)
#3
2
you can use egrep (which is basically the same as grep -E)
in egrep there are named groups for character classes, e.g.: "digit"
(which makes the command longer in this case - but you get the point...)
您可以使用鹭(基本上与grep - e相同)在鹭中有针对字符类的命名组,例如:“digit”(在这种情况下命令更长)。
another thing that is good to know is that you can use brackets to repeat a pattern
另一件值得注意的事情是,您可以使用括号来重复模式
ifconfig | egrep '([0-9]{1,3}\.){3}[0-9]{1,3}'
or
或
ifconfig | egrep '([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}'
if you only care about the actual IP address use the parameter -o to limit output to the matched pattern instead of the whole line:
如果您只关心实际的IP地址,请使用参数-o将输出限制为匹配的模式,而不是整行:
ifconfig | egrep -o '([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}'
...and if you don't want BCast addresses and such you may use this grep:
…如果你不想要BCast地址,你可以使用这个grep:
ifconfig | egrep -o 'addr:([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}' | egrep -o '[[:digit:]].*'
I assumed you were talking about IPv4 addresses only
我以为你只在谈论IPv4地址
#4
2
Just to add some alternative way:
我想补充一些替代方法:
ip addr | grep -Po '(?!(inet 127.\d.\d.1))(inet \K(\d{1,3}\.){3}\d{1,3})'
it will print out all the IPs but the localhost one.
它会打印出所有的“诱导多能性”但localhost。
#5
1
[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}
[0 - 9]{ 1,3 } \。[0 - 9]{ 1,3 } \。[0 - 9]{ 1,3 } \[0 - 9]{ 1,3 }。
#6
1
I don't have enough reputation points to comment, but I found a bug in Steve's "select only valid addresses" regex. I don't quite understand the problem, but I believe I have found the fix. The first command demonstrates the bug; the second one demonstrates the fix:
我没有足够的声望点置评,但我发现一个错误在史蒂夫的正则表达式只选择有效的地址。我不太明白这个问题,但我认为我找到了解决方法。第一个命令显示错误;第二个演示了解决办法:
$ echo "test this IP: 200.1.1.1" |grep -oE "\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"
$ echo "test this IP: 200.1.1.1" |grep -oE "\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"
200.1.1.1
$
#7
1
maybe this, one sed command, just for sports:
也许这个,一个sed命令,只是为了运动:
ip -o -4 addr show dev eth0 | sed 's/.* inet \([^/]*\).*/\1/'
#8
0
One way using sed
. First instruction deletes all characters until first digit in the line, and second instruction saves first IP in group 1 (\1
) and replaces all the line with it:
一种方法使用sed。第一个指令删除所有字符,直到第一位线和第二指令保存第一个IP组1(\ 1)和替换所有的线:
sed -e 's/^[^0-9]*//; s/\(\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*/\1/'
#9
0
This code works nicely and easy too.
这段代码可以很好的和容易。
ifconfig | grep Bcast > /tmp/ip1
cat /tmp/ip1 | awk '{ print $2 }' > /tmp/ip2
sed -i 's/addr://' /tmp/ip2
IPADDRESS=$(cat /tmp/ip2)
echo "$IPADDRESS"
#1
48
Here's one way using grep
:
这里有一个使用grep的方法:
line='inet 192.168.2.13 netmask 0xffffff00 broadcast 192.168.2.256'
echo "$line" | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b"
Results:
结果:
192.168.2.13
192.168.2.256
If you wish to select only valid addresses, you can use:
如果你想只选择有效的地址,您可以使用:
line='inet 192.168.0.255 netmask 0xffffff00 broadcast 192.168.2.256'
echo "$line" | grep -oE "\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"
Results:
结果:
192.168.0.255
Otherwise, just select the fields you want using awk
, for example:
否则,选择你想要的字段使用awk,例如:
line='inet 192.168.0.255 netmask 0xffffff00 broadcast 192.168.2.256'
echo "$line" | awk -v OFS="\n" '{ print $2, $NF }'
Results:
结果:
192.168.0.255
192.168.2.256
Addendum:
附录:
Word boundaries: \b
单词边界:\ b
#2
3
use this regex ((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(?=\s*netmask)
使用这个正则表达式((25[0 - 5]| 2[0 - 9][0 - 4]|(01)?[0 - 9][0 - 9]?)\){ 3 }(25(0 - 5)| 2[0 - 9][0 - 4]|(01)?[0 - 9][0 - 9]?)(? = \ s *子网掩码)
#3
2
you can use egrep (which is basically the same as grep -E)
in egrep there are named groups for character classes, e.g.: "digit"
(which makes the command longer in this case - but you get the point...)
您可以使用鹭(基本上与grep - e相同)在鹭中有针对字符类的命名组,例如:“digit”(在这种情况下命令更长)。
another thing that is good to know is that you can use brackets to repeat a pattern
另一件值得注意的事情是,您可以使用括号来重复模式
ifconfig | egrep '([0-9]{1,3}\.){3}[0-9]{1,3}'
or
或
ifconfig | egrep '([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}'
if you only care about the actual IP address use the parameter -o to limit output to the matched pattern instead of the whole line:
如果您只关心实际的IP地址,请使用参数-o将输出限制为匹配的模式,而不是整行:
ifconfig | egrep -o '([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}'
...and if you don't want BCast addresses and such you may use this grep:
…如果你不想要BCast地址,你可以使用这个grep:
ifconfig | egrep -o 'addr:([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3}' | egrep -o '[[:digit:]].*'
I assumed you were talking about IPv4 addresses only
我以为你只在谈论IPv4地址
#4
2
Just to add some alternative way:
我想补充一些替代方法:
ip addr | grep -Po '(?!(inet 127.\d.\d.1))(inet \K(\d{1,3}\.){3}\d{1,3})'
it will print out all the IPs but the localhost one.
它会打印出所有的“诱导多能性”但localhost。
#5
1
[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}
[0 - 9]{ 1,3 } \。[0 - 9]{ 1,3 } \。[0 - 9]{ 1,3 } \[0 - 9]{ 1,3 }。
#6
1
I don't have enough reputation points to comment, but I found a bug in Steve's "select only valid addresses" regex. I don't quite understand the problem, but I believe I have found the fix. The first command demonstrates the bug; the second one demonstrates the fix:
我没有足够的声望点置评,但我发现一个错误在史蒂夫的正则表达式只选择有效的地址。我不太明白这个问题,但我认为我找到了解决方法。第一个命令显示错误;第二个演示了解决办法:
$ echo "test this IP: 200.1.1.1" |grep -oE "\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"
$ echo "test this IP: 200.1.1.1" |grep -oE "\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"
200.1.1.1
$
#7
1
maybe this, one sed command, just for sports:
也许这个,一个sed命令,只是为了运动:
ip -o -4 addr show dev eth0 | sed 's/.* inet \([^/]*\).*/\1/'
#8
0
One way using sed
. First instruction deletes all characters until first digit in the line, and second instruction saves first IP in group 1 (\1
) and replaces all the line with it:
一种方法使用sed。第一个指令删除所有字符,直到第一位线和第二指令保存第一个IP组1(\ 1)和替换所有的线:
sed -e 's/^[^0-9]*//; s/\(\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}\).*/\1/'
#9
0
This code works nicely and easy too.
这段代码可以很好的和容易。
ifconfig | grep Bcast > /tmp/ip1
cat /tmp/ip1 | awk '{ print $2 }' > /tmp/ip2
sed -i 's/addr://' /tmp/ip2
IPADDRESS=$(cat /tmp/ip2)
echo "$IPADDRESS"