LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-01-2013, 02:00 AM   #16
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191

Twist on Daniel's solution:
Code:
awk -F"[ ,:]+" '/^Oct 31 14/ {print $8,$9}' file
 
2 members found this post helpful.
Old 11-01-2013, 07:51 AM   #17
socalheel
Member
 
Registered: Oct 2012
Location: Raleigh, NC
Distribution: CentOS / RHEL
Posts: 158

Original Poster
Rep: Reputation: 3
Quote:
Originally Posted by grail View Post
Twist on Daniel's solution:
Code:
awk -F"[ ,:]+" '/^Oct 31 14/ {print $8,$9}' file
could you explain what the + is used for in that line?
 
Old 11-01-2013, 08:20 AM   #18
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by socalheel View Post
could you explain what the + is used for in that line?
-F"[ ,:]" says that any one of the specified characters is taken as one field separator.

-F"[ ,:]+" says that any cluster of the specified characters is taken as one field separator.

Illustrative examples:

str="abc@#def@ghi#$jkl"

This ... awk -F"[@#$]" '{print $2}' <<< $str
... produces a null because there is nothing in str between the @ and the #.

This ... awk -F"[@#$]+" '{print $2}' <<< $str
produces def because @# is taken as the first field separator.

Daniel B. Martin

Last edited by danielbmartin; 11-01-2013 at 09:56 AM. Reason: Add ilustrative examples
 
1 members found this post helpful.
Old 11-01-2013, 09:48 AM   #19
socalheel
Member
 
Registered: Oct 2012
Location: Raleigh, NC
Distribution: CentOS / RHEL
Posts: 158

Original Poster
Rep: Reputation: 3
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
 
Old 11-01-2013, 11:59 AM   #20
socalheel
Member
 
Registered: Oct 2012
Location: Raleigh, NC
Distribution: CentOS / RHEL
Posts: 158

Original Poster
Rep: Reputation: 3
nevermind, figured it out.

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
 
Old 11-02-2013, 02:01 AM   #21
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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
 
1 members found this post helpful.
Old 11-02-2013, 02:31 PM   #22
socalheel
Member
 
Registered: Oct 2012
Location: Raleigh, NC
Distribution: CentOS / RHEL
Posts: 158

Original Poster
Rep: Reputation: 3
i figured as much

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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Grab time from string kram66 Programming 1 05-10-2009 09:23 PM
Efficient use of C string libraries with C++ strings? R00ts Programming 4 04-08-2008 11:43 AM
C: storing string which is more efficient. debiant Programming 22 09-01-2006 12:39 AM
Grab text lines in text file LULUSNATCH Programming 1 12-02-2005 10:55 AM
Manipulating SIP msg string sti2envy Linux - Security 5 10-12-2005 07:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration