0

I have log file. I want extract email list from specific lines. Sample log file:

05:06:48,311  INFO http-nio-8080-exec-81 controller.IndexController:221 - Attempt to login: [xxx@yyy.com]. Headers: ["content-length": "52", ...]
15:06:48,311  INFO http-nio-8080-exec-81 controller.IndexController:221 - Attempt to login: [xxx@yyy.com]. Headers: ["content-length": "52", ...]
09:40:21,187  INFO http-nio-8080-exec-31 security.AuthAuthenticationProvider:81 - User: [16167]. Wrong code. Telegram: [0]. Google: [0]

My script:

cat file.log | sed -r 's/.*Attempt to login: \[(.*)\]\..*/\1/' | sort | uniq > file.log.filtered.txt

I get file.log.filtered.txt:

09:40:21,187  INFO http-nio-8080-exec-31 security.AuthAuthenticationProvider:81 - User: [16167]. Wrong code. Telegram: [0]. Google: [0]
xxx@yyy.com

Why I have line with "AuthAuthenticationProvider" in filtered file?

5

Because you are running your sed command only on lines which contains : Attempt to login so other lines will stay untouched.

You can change your code like this:

grep 'Attempt' file.log | sed -r 's/.*Attempt to login: \[(.*)\]\..*/\1/' | sort | uniq > file.log.filtered.txt

so it exclude the other lines first, then extracts email addresses.

or this one:

sed -re 's/.*Attempt to login: \[(.*@.*)\]\..*/\1/;t;d' file.log | sort | uniq

or even this one:

sed -nr 's/.*Attempt to login: \[(.*@.*)\]\..*/\1/p' file.log | sort | uniq

Or just use grep:

grep 'Attempt' file.log | grep -o '[[:alnum:]+\.\_\-]*@[[:alnum:]+\.\_\-]*' | sort | uniq

Regex for last command form here

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.