Shell Script to Filter Output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell Script to Filter Output
# 1  
Old 05-10-2010
Shell Script to Filter Output

Hi guys - I use this script below to search for files that are of todays date. It also informs if the file doesnt exist or is old.

I am trying to make the script inform of the errors in the "begining" of the output email. Such as all errors are grouped together.

I am using the following code below in red to acheive this, however its not working exactly as I want it.

Can someone guide me? (thanks to all those who have helped me debug this one before)

Code:
#!/bin/bash
NOW=$(date +"%Y-%m-%d")
LOGFILE=/tmp/myemail.txt

# List the directories and files to process. 
DIR=( "10 hosts are listed here" )
FILE=( List file names here, without directories or slashes )

#############################
# Define functions
#############################

function checkStat
{
    # make sure stat command is installed
    which stat > /dev/null
    if [ $? -eq 1 ]; then
        echo "stat command not found!"
        exit 2
    fi
}

function processFile
{
    if [ -f ${1} ]; then
        VAR="$(stat -c %y ${1})"
        VAR2="${VAR:0:10}"

        if [ $NOW == $VAR2 ]; then
            echo "Current File Found: ${1}" | tee -a ${LOGFILE}
        else
            erroremail=1
            echo "Old File Found: ${1}" | tee -a ${LOGFILE}
        fi
    else
        erroremail=1
        echo "File Not Found: ${1}" | tee -a ${LOGFILE}
    fi
    dirp1=${DIR%*/*/*} ; dirp1=/${dirp1#*/*/}
    dirp2=${DIR#/*/*/*/*}

}

#############################
# Main
#############################

# Verify the stat command exists
checkStat

# Create or clear the log file
echo "" > ${LOGFILE}

# Process the files
for d in ${DIR[@]}; do
    for f in ${FILE[@]}; do
        processFile "${d}/${f}"
    done
done

#############
#Email 
#############

if [ $erroremail -eq 1 ] ; then
       SUBJECT="ERROR: File Problem"
         else
              SUBJECT="OK: File Current"
fi

# Email To ?
EMAIL="somebody@nowhere.com"
# Email text/message
EMAILMESSAGE="/tmp/myemail.txt"
# send an email using /bin/mail
mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE

# 2  
Old 05-10-2010
A simple solution would be to preface each line with a number that will specify where in the final email you want the line to appear. ie:

Code:
200 File not found: frogs
100 File older than 1 day: dogs
400 File just created: logs

Then redirect all that out of your for loop:

Code:
for x in blah ; do

 ....

...

done | sort | cut -f2- > mail.tmp

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script to filter certain lines from a file

hi~ i need script on AIX. and have a text file following : create aa 1 2 3 from a@a; create bb from b; create cc 3 4 5 6 6 7 from c@c; (7 Replies)
Discussion started by: tomato00
7 Replies

2. Shell Programming and Scripting

Shell script to filter records in a zip file that contains matching columns from another file

Not sure if this is the correct forum for this question. I have two files. file1.zip, file2 Input: file1.zip col1, col2 , col3 a , b , 0:0:0:0:0:c436:9346:d40b x, y, 0:0:0:0:0:880:39f9:c9a7 m, n , 0:0:0:0:0:80c7:9161:fe00 file2.txt col1 c4:36:93:46:d4:0b... (1 Reply)
Discussion started by: anil.v
1 Replies

3. Shell Programming and Scripting

Help with shell script - filter txt file full of ips

Hello again gentlemen. I would like to make a shell script to 'optimize' a plain text full of IPs. Let's suppose to have this text file: 1.192.63.253-1.192.63.253 1.0.234.46/32 1.1.128.0/17 1.116.0.0/14 1.177.1.157-1.177.1.157 1.23.22.19 1.192.61.0-1.192.61.99 8.6.6.6 I want to... (2 Replies)
Discussion started by: accolito
2 Replies

4. Shell Programming and Scripting

Shell Script (ksh) - SQLPlus query filter using a string variable

Using ksh, I am using SQLPlus to execute a query with a filter using a string variable. REPO_DB=DEV1 FOLDER_NM='U_nmalencia' FOLDER_CHECK=$(sqlplus -s /nolog <<EOF CONNECT user/pswd_select@${REPO_DB} set echo off heading off feedback off select subj_name from subject where... (5 Replies)
Discussion started by: nkm0brm
5 Replies

5. UNIX for Dummies Questions & Answers

Shell script to read lines in a text file and filter user data Shell Programming and Scripting

sxsaaas (3 Replies)
Discussion started by: VikrantD
3 Replies

6. Shell Programming and Scripting

How the filter output?

Hey, I'm using some sensors that can be read by http. If I run following command: curl -v 'http://192.168.111.23:8080/sensor/52f5c63cc4221fbbedcb499908a0a823?version=1.0&interval=minute&unit=watt&callback=realtime' I'm getting: I would like to put this now in a sheet with only the... (9 Replies)
Discussion started by: brononius
9 Replies

7. Shell Programming and Scripting

Shell Script to filter users and create them again from a back-up server

This is a script to filter the users out of etc/passwd and etc/group. So if you want to migrate of restore a server you can use this script from a backup to restore and make the same users on you had.. Please feedback and comments. #!/bin/bash prompt_list () { # haal uit de argumenten de... (5 Replies)
Discussion started by: dannyvdberg
5 Replies

8. Shell Programming and Scripting

Filter data in Excel sheet using Shell Script

Hi, I have an excel sheet which has 100000 records. All these records are having 3 columns each with the last column as "Y" or "N". I would like to filter those records which has the value "Y". Can you please let me know how to proceed with that? Thanks in advance. -Sri ----------... (8 Replies)
Discussion started by: bhanusri83
8 Replies

9. Shell Programming and Scripting

Shell script to read lines in a text file and filter user data

hi all, I have this file with some user data. example: $cat myfile.txt FName|LName|Gender|Company|Branch|Bday|Salary|Age aaaa|bbbb|male|cccc|dddd|19900814|15000|20| eeee|asdg|male|gggg|ksgu|19911216||| aara|bdbm|male|kkkk|acke|19931018||23| asad|kfjg|male|kkkc|gkgg|19921213|14000|24|... (4 Replies)
Discussion started by: srimal
4 Replies

10. Shell Programming and Scripting

Shell Script for searching files with date as filter

Hi , Assume today's date is 10-May-2002. I want to get a list of files which were last modified since 01-May-2002. If I run the script after 5 days, it should still list me the files modified from 01-May-2002 till today. I also plan to pass the date 01-May-2002 as an argument to the shell script... (3 Replies)
Discussion started by: kanakaraj_s
3 Replies
Login or Register to Ask a Question