This is for an assignment so I have no choice but to use sed.
这是一个任务,所以我别无选择,只能使用sed。
Given a file messages, how can I extract all the IP addresses and print them?
给定文件消息,如何提取所有IP地址并打印它们?
I first tried
我第一次尝试
sed -n '/((1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.){3}(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])/p' messages
But it printed nothing. After doing some research, I found out that sed does not support non-greedy operators like ? and |.
但它什么都没印。经过一番研究,我发现sed不支持非贪婪的运营商吗?和|。
I've been wracking my brain but I can't think of a way to do this without the non-greedy operators. How can I do this?
我一直在捣乱我的大脑,但如果没有非贪婪的操作员,我想不出办法做到这一点。我怎样才能做到这一点?
4 个解决方案
#1
7
Use sed -r
(extended regex) or escape the capture groups with \
使用sed -r(扩展正则表达式)或使用\转义捕获组
#2
23
grep will be more suitable there (if you have sed
, you should have grep
too):
grep会更适合那里(如果你有sed,你也应该有grep):
grep -oE '((1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.){3}(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])' messages
This is your own regex
with no modification (tested OK)
这是你自己的正则表达式,没有修改(测试OK)
#3
9
If you have GNU sed
, you could simply add the -r
flag to use EREs:
如果你有GNU sed,你可以简单地添加-r标志来使用ERE:
sed -rn '/((1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.){3}(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])/p' file
Otherwise, you will need to escape certain characters:
否则,您将需要转义某些字符:
sed -n '/\(\(1\?[0-9][0-9]\?\|2[0-4][0-9]\|25[0-5]\)\.\)\{3\}\(1\?[0-9][0-9]\?\|2[0-4][0-9]\|25[0-5]\)/p' file
These characters include:
这些字符包括:
- groups using parenthesis:
(
,)
- 使用括号的组:(,)
- occurrence braces:
{
,}
- 发生括号:{,}
- 'or' pipes:
|
- '或'管道:|
- non-greedy question marks:
?
- 非贪婪的问号:?
Generally (although not for your case) I use the following to match IP address:
一般情况下(虽然不适合您的情况)我使用以下内容来匹配IP地址:
sed -rn '/([0-9]{1,3}\.){3}[0-9]{1,3}/p' file
Or in compatibility mode:
或者在兼容模式下:
sed -n '/\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}/p' file
#4
0
You can do that too:
你也能做到:
Windows:
视窗:
ipconfig | perl -nle'/(\d+\.\d+\.\d+\.\d+)/ && print $1' | sed '2 d' | head -n1;
OSX:
OSX:
ifconfig | perl -nle'/(\d+\.\d+\.\d+\.\d+)/ && print $1' | sed '1 d' | head -n1;
#1
7
Use sed -r
(extended regex) or escape the capture groups with \
使用sed -r(扩展正则表达式)或使用\转义捕获组
#2
23
grep will be more suitable there (if you have sed
, you should have grep
too):
grep会更适合那里(如果你有sed,你也应该有grep):
grep -oE '((1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.){3}(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])' messages
This is your own regex
with no modification (tested OK)
这是你自己的正则表达式,没有修改(测试OK)
#3
9
If you have GNU sed
, you could simply add the -r
flag to use EREs:
如果你有GNU sed,你可以简单地添加-r标志来使用ERE:
sed -rn '/((1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.){3}(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])/p' file
Otherwise, you will need to escape certain characters:
否则,您将需要转义某些字符:
sed -n '/\(\(1\?[0-9][0-9]\?\|2[0-4][0-9]\|25[0-5]\)\.\)\{3\}\(1\?[0-9][0-9]\?\|2[0-4][0-9]\|25[0-5]\)/p' file
These characters include:
这些字符包括:
- groups using parenthesis:
(
,)
- 使用括号的组:(,)
- occurrence braces:
{
,}
- 发生括号:{,}
- 'or' pipes:
|
- '或'管道:|
- non-greedy question marks:
?
- 非贪婪的问号:?
Generally (although not for your case) I use the following to match IP address:
一般情况下(虽然不适合您的情况)我使用以下内容来匹配IP地址:
sed -rn '/([0-9]{1,3}\.){3}[0-9]{1,3}/p' file
Or in compatibility mode:
或者在兼容模式下:
sed -n '/\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}/p' file
#4
0
You can do that too:
你也能做到:
Windows:
视窗:
ipconfig | perl -nle'/(\d+\.\d+\.\d+\.\d+)/ && print $1' | sed '2 d' | head -n1;
OSX:
OSX:
ifconfig | perl -nle'/(\d+\.\d+\.\d+\.\d+)/ && print $1' | sed '1 d' | head -n1;