Link to home
Start Free TrialLog in
Avatar of jasgot
jasgot

asked on

How to format grep statement?

I have a grep command that looks through the maillog and tells me how what source IPs are logging in to email.

The problem is that the output  has no <CR><LF>s in the file so it is a huge runon sentence. I am looking for the additional syntax that will at least give a <CR><LF> after every line, or a <br /> since I am looking at this file from a url link.  It would also be nice to have a blank line after every email address group.

This is my grep statement:
grep 'pop3-login' /var/log/maillog | grep ': Login: user' | sed -e 's/.*user=<//' -e 's/>.*rip=/ /' -e 's/, lip.*$//' | sort | uniq -c >/home/domain/public_html/email_logins.html

This is what I get:
12 adaml@domain1.com xxx.228.235.254 1 adaml@domain1.com xxx.134.196.144 524 adaml@domain1.com xxx.14.122.184 523 afletcher@domain2.com xxx.78.29.0 5551 afletcher@domain2.com xxx.83.195.76 1061 aglenn@domain3.com xxx.110.64.230 302 akastrati@domain3.com xxx.197.96.69

This is what I would like to see:

12 adaml@domain1.com xxx.228.235.254
1 adaml@domain1.com xxx.134.196.144
524 adaml@domain1.com xxx.14.122.184

523 afletcher@domain2.com xxx.78.29.1
5551 afletcher@domain2.com xxx.83.195.76

1061 aglenn@domain3.com xxx.110.64.230

302 akastrati@domain3.com xxx.197.96.69


Thanks.
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
after the uniq -c add
| awk ' { print $1,$2,$3,"\n"}' >> to your file
You can try to replace the carriage return and the line feed with a blank line.

perl -pi -e 's/\r\n/\n/g'

Open in new window

did not notice that you were outputing to an html page.
from a security point of view do you really want to publish your email addresses?

write to a .txt file instead of html
Avatar of skullnobrains
skullnobrains

ozo is right

given then fact hat sort and uniq work, your file is multi-line

several options there

1) html-ize the file
.... uniq -c | sed 's/$/<br>/' >> output file

2) have your web server print the file as text and not html. since any decent web server would do it automagically, i assume you have some kind of script that sends the file to the client. you'd need to instruct it to print a text/plain header and check for line endings if you expect to view it on windows. you can stick unixtodos or tofrodos in the pipe if you need to change line endings

3) you can print the output between <pre>...</pre>
Avatar of jasgot

ASKER

PERFECT! Thank you.