?
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
Medium Priority
?
824 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 800 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 400 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 68

Assisted Solution

by:woolmilkporc
woolmilkporc earned 800 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 800 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 800 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
 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

801 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