Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm kinda a newbie to the whole Linux thing but I'm having a doozie of a time trying to figure out how to sift through a my qmail log files.

At this point, what I want to do is do a search that outputs just what is inside the square brackets after qmail-local-handlers. So, for example, taking the first line in my log snippet ...

(Oct  3 10:17:21 125388-web2 qmail-local-handlers[18145]: to=test@emailaddress.com)

... I would like for it to output 18145.

I'm not sure what commands I could run to get the result I'm looking for. Can anyone help?

Here is some sample data I'm using if it will help. Many thanks!!

Oct  3 10:17:21 125388-web2 qmail-local-handlers[18145]: to=test@emailaddress.com|
Oct  3 10:29:14 125388-web2 qmail-local-handlers[22908]: to=test@emailaddress.com|
Oct  3 10:29:30 125388-web2 qmail-local-handlers[23017]: to=test@emailaddress.com|
Oct  3 10:30:58 125388-web2 qmail-local-handlers[23815]: to=test@emailaddress.com|
Oct  3 10:31:04 125388-web2 qmail-local-handlers[23861]: to=test@emailaddress.com|
Oct  3 12:06:52 125388-web2 qmail-local-handlers[30174]: to=test@emailaddress.com|
Oct  3 12:07:03 125388-web2 qmail-local-handlers[30240]: to=test@emailaddress.com|
Oct  3 12:19:05 125388-web2 qmail-local-handlers[3243]: to=test@emailaddress.com|
share|improve this question

2 Answers 2

up vote 0 down vote accepted

This will grab the digits between the (first) pair of square brackets:

sed "s/.*\[\([0-9]\+\)\].*/\1/" logfile
share|improve this answer

Have you tried:

cut -d '[' -f 2 LOGFILE | cut -d ']' -f 1
share|improve this answer
    
that was short :) –  warren Oct 7 '09 at 4:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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