Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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]:

... 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]:|
Oct  3 10:29:14 125388-web2 qmail-local-handlers[22908]:|
Oct  3 10:29:30 125388-web2 qmail-local-handlers[23017]:|
Oct  3 10:30:58 125388-web2 qmail-local-handlers[23815]:|
Oct  3 10:31:04 125388-web2 qmail-local-handlers[23861]:|
Oct  3 12:06:52 125388-web2 qmail-local-handlers[30174]:|
Oct  3 12:07:03 125388-web2 qmail-local-handlers[30240]:|
Oct  3 12:19:05 125388-web2 qmail-local-handlers[3243]:|
share|improve this question
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


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.