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
 DD063290F38
 D8E42291DBE
 D440F291197
 D3E14291D93
. . .

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  sssspc@xx.sg
(host mail.phins.com.sg[119.73.200.204] said: 452 <jason@phins.com.sg> Mailbox size limit exceeded (in reply to RCPT TO command))
                                         jason@phins.com.sg

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

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

D3E14291D93     3051 Tue Mar  1 10:19:51  sssspc@xx.sg
(host mailserv.skylightgroup.com[202.52.100.7] said: 452 Message for <florenceteh@melandas.com.sg> would exceed mailbox quota (in reply to RCPT TO command))
                                         florencet@meland.com.sg

9B90CA58AE     1278 Sun Feb 27 15:28:08  contact_autoreply@xx.sg
           (connect to park.goldenname.com[75.125.148.76]: Connection refused)
                                         Manue@3nan.com

E7EEBA579E     1272 Sun Feb 27 19:48:56  contact_autoreply@xx.sg
(host alt1.aspmx.l.google.com[74.125.65.27] 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
sunhuxAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
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



wmp
0
 
MarioAlcaideCommented:
You could do something like that:


---
#!/bin/bash

grep your_text_string your_file_name | head -1

---

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

Regards
0
 
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
0
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 -
0
 
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:)
do
      (( T = $RECNUM - 1 ))
      mailq|sed -n "${T},${T}P"  |awk '{print $1}'
done  | postsuper -h -



0
 
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

#!/usr/bin/ksh
TMPFILE=/tmp/mq_scan.$$
mailq > $TMPFILE
if [ -s $TMPFILE ]
then
    for RECNUM in $(grep -n -f LookUpList $TMPFILE |cut -f1 -d:)
    do
        (( T = $RECNUM - 1 ))
        sed -n "${T},${T}P" $TMPFILE |awk '{print $1}'
    done  | postsuper -h -
fi
rm $TMPFILE
0
 
woolmilkporcCommented:
Nice!

wmp
0
 
woolmilkporcCommented:
By the way, Tomunique,

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

0
 
TomuniqueCommented:
Yup.. thanks...  

0
 
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
0
 
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
0
 
sunhuxAuthor Commented:
excellent
0
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.