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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
02-01-2006, 01:57 PM
|
#1
|
|
Senior Member
Registered: Feb 2004
Location: Chicago
Distribution: CentOS
Posts: 1,180
Rep:
|
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?
|
|
|
|
02-01-2006, 02:51 PM
|
#2
|
|
Senior Member
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,591
|
From your example,
grep "host=" /var/log/maillog | awk -F\= '{print $2}'
|
|
|
|
02-01-2006, 04:29 PM
|
#3
|
|
Moderator
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,903
|
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.
|
|
|
|
02-02-2006, 02:22 AM
|
#4
|
|
Member
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383
Rep:
|
one more,
Code:
sed -n '/host/p' maillog | sed -e 's/^.*host=//;s/ ip.*$//'
|
|
|
|
02-02-2006, 03:58 AM
|
#5
|
|
Member
Registered: Dec 2004
Location: Québec
Distribution: Gentoo, Kubuntu Karmic
Posts: 48
Rep:
|
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
|
|
|
|
02-02-2006, 11:25 AM
|
#6
|
|
Moderator
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,903
|
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
|
|
|
|
02-03-2006, 10:59 AM
|
#7
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
my go
Code:
perl -ne 'print "$&\n" while m#\d+\.\d+\.\d+\.\d+#g'
|
|
|
|
03-09-2006, 10:22 AM
|
#8
|
|
Member
Registered: Dec 2004
Location: Québec
Distribution: Gentoo, Kubuntu Karmic
Posts: 48
Rep:
|
Wow ! That perl line is EXCELLENT !
Keeping it myself 
Thx bigearsbilly 
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 10:50 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|