I need to write a script where the particular string(alphanumeric) should be searched inside the file with every hour for whole day. And the search should not contain the previous hour results.

Time stamp format inside file is "2014-02-17 17:00:01"

The below command will give the out put for the whole day count but i need every hour count.

grep "recFileChg:true" *.tmp | awk -F "|" '{print $4, $29}' | cut -d "," -f1,2 | awk -F "[" '{print $1, $2}'

I would be very much appreciated if somebody can help me on this.


Well, it would be nice to have a line you have and a line you would like to get as the search result.

Anyway, I think it can be solved with just grep and cut:

grep "recFileChg:true" *.tmp | cut -f2- -d':' | cut -f2 -d'[' | cut -f1 -d':'
  • the first cut will strip the file name from the grep output
  • the second cut will strip everything up to the '[' character
  • the last cut will cut out the date and the hour
  • I think your are mistaken me, the script should run every one hour and it should search the particular string for the previous hour. Say for example the scripts is running on 16:00 and it should find the string between 15:00 to 16:00 time stamp accordingly the same should be happening for the 24hrs. – user197719 Feb 17 '14 at 16:18
  • 1
    Well, in this case the task is more complex and you didn't provide enough data. If I were you I'd implement it like the following: using find locate the most recent log file, then grep for 'YYYY-MM-DD HH' (where HH is one hour less than the current hour), and grep the resulting stream for the string you want to check. – galaxy Feb 17 '14 at 18:00

In order to accomplish what you want, a simpler method would be to record the number of lines in this file at the end of your script execution and store it in a file. Next time, i.e. the subsequent hour's run, can set the search scope from this line number +1, to the end of the file. A crude example could be as follows:

# assuming the number of lines in file at the last run was recorded 
# in a file called `lastrun`

s=$(cat lastrun)

sed -e "1,${s}d" > searchscope
grep ...blah-blah | cut .. blah-blah searchscope
rm  searchscope

cat myfile|wc -l > lastrun

and put it in a hourly loop or place it in a cron as your heart desires.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.