开发者

Filter apache log file using regular expression

开发者 https://www.devze.com 2023-01-16 17:21 出处:网络
I have a big apache log file and I need to filter that and leave only (in a new file) the log from a certain IP: 192.168.1.102

I have a big apache log file and I need to filter that and leave only (in a new file) the log from a certain IP: 192.168.1.102

I try using this command:

sed -e "/^192.168.1.102/d" < input.txt > output.txt

But "/d" removes those entries, and开发者_如何转开发 I needt to leave them.

Thanks.


What about using grep?

cat input.txt | grep -e "^192.168.1.102" > output.txt

EDIT: As noted in the comments below, escaping the dots in the regex is necessary to make it correct. Escaping in the regex is done with backslashes:

cat input.txt | grep -e "^192\.168\.1\.102" > output.txt


sed -n 's/^192\.168\.1\.102/&/p'

sed is faster than grep on my machines


I think using grep is the best solution but if you want to use sed you can do it like this:

sed -e '/^192\.168\.1\.102/b' -e 'd'

The b command will skip all following commands if the regex matches and the d command will thus delete the lines for which the regex did not match.

0

精彩评论

暂无评论...
验证码 换一张
取 消