Shell or Perl script to search for a list of text / string & return 1st string of the preceding line

' mailq ' often returns various problem mails : such mails would hog genuine
mails from going thru

I need to search for a list of text / string that's found from "mailq" output
& then the script would return the first value of the line just preceding the
line these text were found.

List of search text / string are (which may be added in future ):

a) "Connection timed out"
b) "lowest numbered MX record"
c) "Connection refused"
d) "Mailbox size limit exceeded"
e) "Undeliverable address"
f) "Network is unreachable"
g) "exceed mailbox quota"
 .....  this list of search text may grow .....

& based on the sample mailq output below, the script would return the values
. . .

These values are the problem mails' ids & will be passed to postsuper to
be withheld in 'hold' queue :
mailq | script_needed | postsuper -h -

Sample problems mails returned by mailq :
. . . . . .

DD063290F38     2924 Tue Mar  1 10:17:13
(host[] said: 452 <> Mailbox size limit exceeded (in reply to RCPT TO command))

D8E42291DBE     2919 Tue Mar  1 10:20:43
(connect to ritzmail.RITZ-CARLTON.COM.SG[]: Connection timed out)

D440F291197     2185 Fri Feb 25 15:15:24
(Host or domain name not found. Name service error for name=GMAIL.COM.SG type=MX: Host not found, try again)

D3E14291D93     3051 Tue Mar  1 10:19:51
(host[] said: 452 Message for <> would exceed mailbox quota (in reply to RCPT TO command))

9B90CA58AE     1278 Sun Feb 27 15:28:08
           (connect to[]: Connection refused)

E7EEBA579E     1272 Sun Feb 27 19:48:56
(host[] said: 450-4.2.1 The user you are trying to contact is receiving mail at a rate that 450-4.2.1 prevents additional messages from being delivered. Please resend your 450-4.2.1 message at a later time. If the user is able to receive
Who is Participating?

Improve company productivity with a Business Account.Sign Up

woolmilkporcConnect With a Mentor Commented:
mailq | grep -B1 -f pattern_file |grep -v -f pattern_file |grep -v -e "--" | cut -f1 -d " "

pattern_file should just contain the search strings, without surrounding qotes:

Connection timed out
lowest numbered MX record
Connection refused
Mailbox size limit exceeded
Undeliverable address
exceed mailbox quota

You could do something like that:


grep your_text_string your_file_name | head -1


This will search into your file a certain string, and just show the first coincidence.

svsConnect With a Mentor Commented:
mailq | egrep -B1 '(Connection timed out|Undeliverable address|Mailbox size limit exceeded)' | awk '/^[0-9A-F]+/ {print $1}'

you get the idea
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

woolmilkporcConnect With a Mentor Commented:
Forgot the remainder of your script:

mailq | grep -B1 -f pattern_file |grep -v -f pattern_file |grep -v -e "--" | cut -f1 -d " " | postsuper -h -
TomuniqueConnect With a Mentor Commented:
And, for those of us on systems like AIX, where -B is not an option for grep, here's a work around

for RECNUM in $(mailq|grep -n -f LookUpList |cut -f1 -d:)
      (( T = $RECNUM - 1 ))
      mailq|sed -n "${T},${T}P"  |awk '{print $1}'
done  | postsuper -h -

TomuniqueConnect With a Mentor Commented:
Reviewing this... if you have a very busy mail system... the results of the multiple mailq commands may be inconsistent.. so here's a more stable option

mailq > $TMPFILE
if [ -s $TMPFILE ]
    for RECNUM in $(grep -n -f LookUpList $TMPFILE |cut -f1 -d:)
        (( T = $RECNUM - 1 ))
        sed -n "${T},${T}P" $TMPFILE |awk '{print $1}'
    done  | postsuper -h -

By the way, Tomunique,

there is GNU grep for AIX (, the toolbox, and more).

Yup.. thanks...  

sunhuxAuthor Commented:

I think Mario's solution is not what I wanted :

> " just show the first coincidence"
What I need is one 1 line above the line that contains the search string.

For solution of the others, I can only test out on Monday as my Linux VM
at home is crashed, so can only test in the ofc
sunhuxConnect With a Mentor Author Commented:

Like what WMP & Tomunique had discussed
if there's no GNU grep to perform
   grep -B 1 "No value found" filename

then we'll do (like in AIX)
   sed -n -e 'N; /No Value Found/p' yourfile.txt

Great replies guys.

I'll close this thread tomorrow
sunhuxAuthor Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.