Search File Script

Nice forum for noobs like me to UNIX. With that said, I need assistance.

I am trying to make a script to search files for a specific date and string within the file. Then I need the script to move the file that have the specific date and string to a directory. In the interim I have been manually moving the files by GREP'ing for the string, then by GREP'ing for the date in the new DIR. It takes a bunch of time.

Thanks in advance!

EDIT: I am using KSH.

Here is a simple script:

#!/usr/bin/ksh
filedate="$2 $1"
pattern=$3
cd /path/to/files
ls -l |grep "$filedate" | awk '{print $9}'|while read filename; do
   grep a $filename > /dev/null && mv $filename /path/to/dest/
done

Try this with changes in the necessary places.
Run it as:
scriptname date month(in letters) pattern_to_search
eg. scriptname 11 Jan foo
This will search all files in particular directory with date stamp of 11th Jan for occurance of the pattern foo and move them to the specified path.

--not tested--

Thanks for the help blowtorch...

Does that script grep the pattern? I ran the script and it pulled all the files with a file date correctly, but it did not use the pattern I selected. (the string was not found in the file text)

EDIT: I am looking at the results... Is there a way to select the year as well? Would I have to make a third variable in the filedate?

I think this will serve your purpose:

#! /usr/bin/ksh
#set -x                 # unhash to debug

print_usage() {
        echo "usage: ./searchnmove <date_to_search> <pattern_to_search_for> <src_dir> <dest_dir>

date_to_search can have pattern of YYYYMMDD or just MMDD. In the second
               case the current year will be assumed"
}

PERLDIR=/opt/perl/bin   # set this to the directory where perl is installed

if [ $# -ne 4 ]; then
        echo "searchnmove: incorrect number of arguments"
        print_usage
        exit 2
fi

if [ ${#1} -eq 8 ]; then
        searchdate=$1
elif [ ${#1} -eq 4 ]; then
        searchdate=$(date +%Y)$1
else
        echo "searchnmove: date pattern incorrect"
        print_usage
        exit 2
fi
if [ ! -d $3 ]; then
        echo "searchnmove: source directory does not exist"
        exit 2
fi
SRCDIR=$3
if [ ! -d $4 ]; then
        echo "searchnmove: destination directory does not exist"
        exit 2
fi
DESTDIR=$4
searchpattern="$2"
cd $SRCDIR
for file in *; do
        filemod=$($PERLDIR/perl -w -e '$mtime=(stat($ARGV[0]))[9]; @ltime=localtime $mtime; printf "%04s%02s%02s",$ltime[5]+1900,$ltime[4]+1,$ltime[3];' $file)
        if [ $filemod = $searchdate ]; then
                grep $searchpattern $file >/dev/null && mv $file $DESTDIR/
        fi
done

Cheers!

Thanks a million Blowtorch, the script works great. I just have a final question, I sometimes run the script for a span of a few days, so i tried to modify the command to:

./searchnmove 2005122[0-9] <pattern_to_search_for> <src_dir> <dest_dir>

It failed. How should I modify the script to query for more than one day?

Thanks!

You are expecting the script to expand the date like a shell expands filenames. Sorry, that does not happen. If you do want to run the script for multiple dates, you can try something like:

for i in 0 1 2 3 4 5 6 7 8 9; do
   ./searchnmove 2005122${i} ### rest of the args 
done

This will run the script once for each date.

My mistake. Sounds great. Thanks again for your help.

After searching for a specified string, I would like to delete (instead of moving the file as mentioned in this thread) couple of rows from the file and continue searching. Basically, I would like to search through a text file that holds logs with date-time stamp in them and then clean up the file if the log entry is more than 2 days old.

Example log entries:

02.01.06 03:08:20.040 Output: 34 Bytes ==> 4472
???"?????????__ABCL__??__ABCL__�?

02.05.06 03:08:20.040 Output: 34 Bytes ==> 4472
???"?????????__CEDL__??__CEDL__�?

In the above example, I would like to delete the entry from 02.01 and keep 02.05 if today is 02.05.06

Thanks.