Search File Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search File Script
# 1  
Old 01-11-2006
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.

Last edited by I4ce; 01-11-2006 at 04:57 PM..
# 2  
Old 01-11-2006
Here is a simple script:
Code:
#!/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--
# 3  
Old 01-11-2006
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?

Last edited by I4ce; 01-11-2006 at 06:24 PM..
# 4  
Old 01-11-2006
I think this will serve your purpose:
Code:
#! /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!
# 5  
Old 01-12-2006
Final question

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!
# 6  
Old 01-12-2006
Quote:
Originally Posted by I4ce
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:
Code:
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.
# 7  
Old 01-12-2006
My mistake. Sounds great. Thanks again for your help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to search a particular String form a file a write to another file using perl script

I have file which contains a huge amount of data. I need to search the pattern Message id. When that pattern is matched I need to get abcdeff0-1g6g-91g3-1z2z-2mm605m90000 to another file. Kindly provide your input. File is like below Jan 11 04:05:10 linux100 |NOTICE... (2 Replies)
Discussion started by: Raysf
2 Replies

2. Shell Programming and Scripting

Exclude file from a search script

Hello, I use the following script to delete files from folders. There is an old file I wish to exclude the deletion script. How to exclude a file by name? Current script: ============================= #!/bin/ksh # ---------------------------------------------------- #... (1 Reply)
Discussion started by: pointer
1 Replies

3. Shell Programming and Scripting

Script to search a large file with a list of terms in another file

Hi- I am trying to search a large file with a number of different search terms that are listed one per line in 3 different files. Most importantly I need to be able to do a case insensitive search. I have tried just using egrep -f but it doesn't seam to be able to handle the -i option when... (3 Replies)
Discussion started by: dougzilla
3 Replies

4. Shell Programming and Scripting

search within the file in script

Hi all, I have to search a particular folder which conatins around 2000 XML files. I need to identify those files which have a value GSM under Product Value <item name="Product Value"> <value>GSM</value> </item> I need to copy those file names to another file. This is my... (10 Replies)
Discussion started by: Maya_Pillai
10 Replies

5. Shell Programming and Scripting

To search a file for a specific word in a file using shell script

Hi All, I have a sql output file has below. I want to get the values 200000040 and 1055.49 .Can anyone help me to write a shell script to get this. ACCOUNT_NO ------------------------------------------------------------ BILL_NO ... (8 Replies)
Discussion started by: girish.raos
8 Replies

6. Shell Programming and Scripting

Script which will search for a file for 15 mins

Hi All, I would like to write a script which will search a file say abc.dat in /a/b/data for 15 mins only. If the script finds the file in 15 mins then it will exit will exit sucessfully and if there is no file for 15 mins it will exit and copy the last day file (abc.dat_ddmmyyhhmmss) from... (1 Reply)
Discussion started by: chandancsc
1 Replies

7. UNIX for Dummies Questions & Answers

Shell script to search for text in a file and copy file

Compete noob question.... I need a script to search through a directory and find files containing text string abcde1234 for example and then copy that file with that text string to another directory help please :eek: (9 Replies)
Discussion started by: imeadows
9 Replies

8. Shell Programming and Scripting

script to search a file

hi all i have a requirement to write a script which will serach a file in a particuar location for 3 hours from the time when the script is run.if during this period the file arrives in a particular location then this will transmit the file to a other server.please help me writing the script which... (1 Reply)
Discussion started by: dr46014
1 Replies

9. Shell Programming and Scripting

search for the contents in many file and print that file using shell script

hello have a file1 H87I Y788O T347U J23U and file2 J23U U887Y I99U T556U file3 I99O J99T F557J file4 N99I T666U R55Y file5 H87I T347U file6 H77U R556Y E44T file7 Y788O K98U H8I May be using script we can use file1 to search for all the files and have the output H87I file5... (3 Replies)
Discussion started by: cdfd123
3 Replies

10. Shell Programming and Scripting

File search for pattern - script

Hi All, I have two directories as 1) mi/job -> job1.sh, job2.sh, job3.sh 2) mi/sysin -> sysin1, sysin2, sysin3,sysin4 I want to wrrite a script such that it should accept two parameters as directory paths. $ myscript mi/sysin mi/job. The script should be able to scan all files... (3 Replies)
Discussion started by: rahulrathod
3 Replies
Login or Register to Ask a Question