[SOLVED] text manipulating ... the more efficient way to grab a string
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
so i'm not sure how to resolve what i want to do now.
so my script gives me exactly what i want it to.
this:
Code:
#!/bin/bash
STRING=`grep "Oct 31 11" /var/log/maillog | grep -i spam | egrep -v "avast.com|to=<spam"`
if
test -n "$STRING"
then
MAILID=`grep "Oct 31 11" /var/log/maillog | grep -i spam | egrep -v "avast.com|to=<spam" | awk -F ": " '{print $2}' | sort -u`
for i in $MAILID
do
echo "Postfix ID Number $i"
grep $i /var/log/maillog | grep "sasl_username" | awk -F ", " '{print $3}'; echo -e '\n'
echo "From: "; grep $i /var/log/maillog | grep "from=<" | sed -e 's/from=<>/<NO DATA FOR THIS FIELD>/g' | awk -F "<" '{print $2}' | awk -F ">" '{print $1}'; echo -e '\n'
echo "To: "; grep $i /var/log/maillog | grep "to=<" | sed -e 's/to=<>/<NO DATA FOR THIS FIELD>/g' | awk -F "<" '{print $2}' | awk -F ">" '{print $1}'; echo -e '\n'
done
fi
will produce this:
Code:
Postfix ID Number EFA161FF9F8
sasl_username=first.last@work.com
From:
admin@aroundeugene.com
To:
email1@hotmail.com
email2@hotmail.com
email3@hotmail.com
my goal now is to run this every hour, and if spam is present, send all that output to my email. if it's not present, don't do anything.
can someone direct me down the right path?
Last edited by socalheel; 11-01-2013 at 10:49 AM.
Reason: changed my question
this is designed to put in cron at the 59th minute of each hour to scan the maillog for any instance of the word spam for the current hour. so at 1159, it would grep maillog for every entry for the current day and every entry for the 11th hour (11:00 to 11:59)
Code:
#!/bin/bash
DATE=`date -d today +'%b %e %H'`
STRING=`grep "$DATE" /var/log/maillog | grep -i spam | egrep -v "avast.com|to=<spam"`
if
test -n "$STRING"
then
MAILID=`grep "$DATE" /var/log/maillog | grep -i spam | egrep -v "avast.com|to=<spam" | awk -F ": " '{print $2}' | sort -u`
for i in $MAILID
{
echo "Postfix ID Number $i"
grep $i /var/log/maillog | grep "sasl_username" | awk -F ", " '{print $3}'
echo "From: "; grep $i /var/log/maillog | grep "from=<" | sed -e 's/from=<>/<NO DATA FOR THIS FIELD>/g' | awk -F "<" '{print $2}' | awk -F ">" '{print $1}'
echo "To: "; grep $i /var/log/maillog | grep "to=<" | sed -e 's/to=<>/<NO DATA FOR THIS FIELD>/g' | awk -F "<" '{print $2}' | awk -F ">" '{print $1}'; echo -e '\n'
} > /spam_entries.txt
mail -s "Spam entries have been found in $(hostname)'s maillog" < /spam_entries.txt first.last@work.com
fi
Glad you found a solution You could do the whole thing in awk which would remove the need for bash, grep, date and echo and use a single solution. Of course it may well be suited to Perl, Ruby or Python too
i've only been doing red hat admin for 2 years now and i just started "scripting" about 3 months ago, i'm so far from where i want to be in terms of knowing what i'm doing.
in the meantime, i'll just post my questions here when i need guidance and hopefully you seasoned veterans can give me the right direction.
i do appreciate all the input you guys give me ... it is well appreciated.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.