简体   繁体   English

从文件中提取特定字符串,然后使用grep,awk,sed输出到另一个文件

[英]Extract a particular string from a file and output to another file using grep, awk, sed

I have a file and it contain the ff strings 我有一个文件,它包含ff字符串

2013-09-08 21:00:54 SMTP connection from [78.110.75.245]:5387 (TCP/IP connection count = 20)
2013-09-08 21:00:54 SMTP connection from [188.175.142.13]:34332 (TCP/IP connection count = 20)
2013-09-08 21:45:41 SMTP connection from [58.137.11.145]:51984 (TCP/IP connection count = 20)
2013-09-08 21:49:26 SMTP connection from [109.93.248.151]:22273 (TCP/IP connection count = 20)
2013-09-08 21:49:27 SMTP connection from [37.131.64.203]:7906 (TCP/IP connection count = 20)

What I want to do is extract the IP address only and save it to a file. 我想做的是仅提取IP地址并将其保存到文件中。

I started with this 我从这个开始

sed '^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$' file > ips

But I couldn't make it work. 但是我无法使它工作。

Using awk : 使用awk

awk -F'[][]' '{print $2}' log.file > addresses
78.110.75.245
188.175.142.13
58.137.11.145
109.93.248.151
37.131.64.203

In practice I would probably go with jasonwryan solution but to answer why your sed command doesn't work is because you are using extended regular expression and even perl compliant regular expressions. 实际上,我可能会使用jasonwryan解决方案,但要回答为什么您的sed命令不起作用的原因是,因为您使用的是扩展的正则表达式 ,甚至使用了与perl兼容的正则表达式。 To use ERE with sed you need to explicitly turn it on using -r with GNU sed or -E with BSD variants. 要将ERE与sed一起使用,您需要使用-rGNU sed-E和BSD变体明确地将其打开。 However sed doesn't support PCRE but you can drop the use of non-capturing groups as it doesn't really help here anyway. 但是sed不支持PCRE,但是您可以放弃使用非捕获组,因为这实际上并没有帮助。

As you are just pattern matching grep is probably better then sed : 由于您只是模式匹配,因此grep可能比sed更好:

$ grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' file
78.110.75.245
188.175.142.13
58.137.11.145
109.93.248.151
37.131.64.203  

Notice the anchors also need dropping, that is ^ and $ as the pattern you want to match does not start at the beginning of the string or end at the end. 请注意,锚点也需要删除,即^$因为要匹配的模式不是从字符串的开头或结尾开始。 grep also doesn't support extend regular expression by default so -E is used and -o prints only the matching part of the line not the whole line. grep在默认情况下也不支持扩展正则表达式,因此使用-E-o仅打印该行的匹配部分而不是整个行。

The final problem is you have just given sed and regular expression and a file. 最后的问题是您刚刚提供了sed和正则表达式以及一个文件。 sed is not grep and won't just print out lines that match (although of course it can, this just isn't how you do it) . sed不是grep并且不会仅打印出匹配的行(尽管当然可以,但这不是您的操作方式) An approach would be to use the substitution command s and replace everything before the IP and everything after: 一种方法是使用替代命令s替换IP之前的所有内容和IP之后的所有内容:

$ sed -r 's/.+[[]([^]]+).+/\1/' file
78.110.75.245
188.175.142.13
58.137.11.145
109.93.248.151
37.131.64.203

Regexplanation: Regexplanation:

s    # sed substitute command 
/    # the delimiter marking the start of the regexp
.+   # one or more of any character
[    # start a character class
[    # character class contains a single opening square bracket 
]    # close character class (needed so single [ isn't treated as unclosed)
(    # start capture group
[    # start character class
^]+  # one or more character not an ]
]    # end character class
)    # end capture group 
.+   # one or more of any character
/    # the delimiter marking the end of the regexp and start of replacement
\1   # the first capture group
/    # the delimiter marking the end of the replacement 

Here is a comparison of different regular expression flavours. 是不同正则表达式风格的比较。

您可以使用sed将方括号[]中的内容进行匹配:

sed 's/.*\[\(.*\)\].*/\1/' log.file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM