Solved

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

Posted on 2011-03-03
12
796 Views
Last Modified: 2012-05-11
' 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
0
Comment
Question by:sunhux
  • 4
  • 3
  • 3
  • +2
12 Comments
 
LVL 4

Expert Comment

by:MarioAlcaide
ID: 35028658
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
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 200 total points
ID: 35028706
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
 
LVL 9

Assisted Solution

by:svs
svs earned 100 total points
ID: 35030732
mailq | egrep -B1 '(Connection timed out|Undeliverable address|Mailbox size limit exceeded)' | awk '/^[0-9A-F]+/ {print $1}'

you get the idea
0
 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 200 total points
ID: 35031103
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
 
LVL 6

Assisted Solution

by:Tomunique
Tomunique earned 200 total points
ID: 35036214
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
 
LVL 6

Assisted Solution

by:Tomunique
Tomunique earned 200 total points
ID: 35036269
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 68

Expert Comment

by:woolmilkporc
ID: 35036270
Nice!

wmp
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 35036308
By the way, Tomunique,

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

0
 
LVL 6

Expert Comment

by:Tomunique
ID: 35036366
Yup.. thanks...  

0
 

Author Comment

by:sunhux
ID: 35045942

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
 

Assisted Solution

by:sunhux
sunhux earned 0 total points
ID: 35045958

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
 

Author Closing Comment

by:sunhux
ID: 35120569
excellent
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now