LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-01-2006, 01:57 PM   #1
abefroman
Senior Member
 
Registered: Feb 2004
Location: Chicago
Distribution: CentOS
Posts: 1,257

Rep: Reputation: 53
How can I awk/sed/grep the IPs from the maillog?


# cat /var/log/maillog
Jan 29 16:43:09 server1 pop[8781]: Session Closed host=205.83.122.83 ip= user=john.smith@mydomain realuser=john.smith@mydomain totalxfer=1981
Jan 29 18:00:31 server1 pop[19092]: Login host=172.83.101.213 ip= user=john.smith@mydomain realuser=john.smith@mydomain

I want it to list the IP's only:
# cat /var/log/maillog | awk {'%d.%d.%d.%d}
205.83.122.83
172.83.101.213

My command is not correct though, does anyone know what the correct command would be?
 
Old 02-01-2006, 02:51 PM   #2
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 327Reputation: 327Reputation: 327Reputation: 327
From your example,

grep "host=" /var/log/maillog | awk -F\= '{print $2}'
 
Old 02-01-2006, 04:29 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,978
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
Quote:
Originally Posted by macemoneta
From your example,

grep "host=" /var/log/maillog | awk -F\= '{print $2}'
Why the grep, though? :)
Code:
awk -F"=" '/host/ {sub(" ip","",$2);print $2}' maillog
The sub-bit is because of the appended " ip" in the output.


Cheers,
Tink

Last edited by Tinkster; 02-01-2006 at 04:31 PM.
 
Old 02-02-2006, 02:22 AM   #4
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
one more,

Code:
sed -n '/host/p' maillog | sed -e 's/^.*host=//;s/ ip.*$//'
 
Old 02-02-2006, 03:58 AM   #5
denver1980
Member
 
Registered: Dec 2004
Location: Québec
Distribution: Gentoo, Kubuntu Karmic
Posts: 48

Rep: Reputation: 15
Just in case you have many process using the "host" key work in their output, use the name of the process (pop in this case).
Code:
# Server=pop
# sed -e '/pop/!d' -e 's/^.*host\=//g' -e 's/ .*$//g' /var/log/maillog
 
Old 02-02-2006, 11:25 AM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,978
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
Or one completely without host :}

Code:
egrep -o -e "[0-9]{2,3}\.[0-9]{2,3}\.[0-9]{2,3}\.[0-9]{2,3}" maillog

Cheers,
Tink
 
Old 02-03-2006, 10:59 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
my go

Code:
 perl -ne 'print "$&\n" while m#\d+\.\d+\.\d+\.\d+#g'
 
Old 03-09-2006, 10:22 AM   #8
denver1980
Member
 
Registered: Dec 2004
Location: Québec
Distribution: Gentoo, Kubuntu Karmic
Posts: 48

Rep: Reputation: 15
Wow ! That perl line is EXCELLENT !
Keeping it myself
Thx bigearsbilly
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
awk and sed issues alaios Linux - General 11 03-24-2005 05:33 AM
sed or awk help requested tonyfreeman Programming 7 10-03-2004 12:23 AM
awk/sed help pantera Programming 1 05-13-2004 11:59 PM
Can I use grep inside awk? Helene Programming 6 04-27-2004 12:58 AM
newbie needs help for grep and awk parker Programming 1 08-12-2003 04:24 AM


All times are GMT -5. The time now is 07:31 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration