Languages and Scripting

Re: how to grep a file name out of list of files within time period

 
anas12
Occasional Contributor

how to grep a file name out of list of files within time period

suppose I have lfollowing ist of files:

 

-rw-r--r--   1 appltest   dba           1078 Apr 28 11:27 o40571618.out
-rw-r--r--   1 appltest   dba              0 Apr 28 11:33 o40571615.out
-rw-r--r--   1 appltest   dba           1685 Apr 28 11:38 o40571622.out
-rw-r--r--   1 appltest   dba              0 Apr 28 11:43 o40571620.out
-rw-r--r--   1 appltest   dba              0 Apr 28 11:54 o40571623.out
-rw-r--r--   1 appltest   dba              0 Apr 28 12:04 o40571624.out
-rw-r--r--   1 appltest   dba              0 Apr 28 12:14 o40571625.out
-rw-r--r--   1 appltest   dba              0 Apr 28 12:24 o40571626.out

 

Now I need to know all files between time periods Apr 28 time between 11:38 & 12:04

 

What would be the right command to know this files as per given time periods?

 

Thanks.

 

 

P.S. This thread has been moved from HP-UX > System Administration to HP-UX > languages. - Hp Forum Moderator

8 REPLIES 8
db13
HPE Pro

Re: how to grep a file name out of list of files within time period

You could try the following using awk...

find . -mtime 0 -ls | awk '$10~/11:(3[8-9]|[4-5][0-9])|12:0[0-4]/'

 

For example...

# find . -mtime 0 -ls | awk '$10~/11:(3[8-9]|[4-5][0-9])|12:0[0-4]/'

   73774    1 -rw-r--r--   1 root     sys            5 Apr 28 11:38 ./test
   73775    1 -rw-r--r--   1 root     sys            6 Apr 27 11:52 ./test2
   73771    1 -rw-r--r--   1 root     sys            6 Apr 27 12:04 ./test3

I am an HPE Employee
A quick resolution to technical issues for your HP Enterprise products is just a click away HPE Support Center Knowledge-base

Accept or Kudo

Bill Hassell
Honored Contributor

Re: how to grep a file name out of list of files within time period

Because human-readable timestamps are very difficult to compare especially between months and years, you'll have to do this in several steps. First, your script will have to create two reference files using touch. The first file should have the timestamp of the older range and the other has the newer timestamp. For your requirement:

 

# cd /somedir
# touch -t 201504281138 oldref # touch -t 201504281204 newref

 Now you would use find to locate files that are newer that the oldref file, remember those filenames, then search that list for files that are not newer than the newref file.

 

# NEWER=$(find . -type f -newer oldref)
# find $NEWER ! -newer newref

 This is especially useful in comparing files more than a year old where ls -l changes to MON DAY YEAR rather than MON DAY HR:MIN. The -newer comparison is accurate to the second. Note that the "! -newer newref" test will always include the newref file.

 



Bill Hassell, sysadmin
Hawkeye
HPE Pro

Re: how to grep a file name out of list of files within time period

I do something very similar to Bill e.g.

 

# touch -t 201504281300 /tmp/starttime
# touch -t 201504281350 /tmp/endtime
# find /yourdir -name "*yourfile" -newer /tmp/starttime ! -newer /tmp/endtime

 

I work for HPE
A quick resolution to technical issues for your HPE products is just a click away HPE Support Center Knowledge-base
See Self Help Post for more details


Accept or Kudo

anas12
Occasional Contributor

Re: how to grep a file name out of list of files within time period

Pls explain $10~/11 what exactly its meaning? Thanks.
anas12
Occasional Contributor

Re: how to grep a file name out of list of files within time period

Hi,

 

One more help pls.

 

All files generated at night 11:46 and anyone of these files has a string 'mismatch'.

How I can find this file only?

 

There may be multiple files generated at that time 11:46 night but only one file contain the string 'mismatch' within the file.

I need to know this file only.

 

 

Thanks.

Bill Hassell
Honored Contributor

Re: how to grep a file name out of list of files within time period

>> All files generated at night 11:46...

 

Don't you mean 23:46 for 'night'?

11:46 is about noon rather than night.

 

This is fairly simple:

cd /someDIRtoSEARCH

for MYFILE in $(ls -l | awk '/ 23:46 /{print $NF}')
do
  [[ $(grep -c "mismatch" $MYFILE) -gt 0 ]] && echo "found it: $MYFILE"
done

If "mismatch" might also be MISMATCH, change the grep option to -ic to ignore UPPER/lower differences.

 

 



Bill Hassell, sysadmin
anas12
Occasional Contributor

Re: how to grep a file name out of list of files within time period

Dear Bill,

 

Your latest post is very much working as per my requirement.  I need one modification in the script.

It should only pick up the latest date file when the script runs because earlier dates similar files are also present in the directory.

 

Thanks.

Dennis Handly
Acclaimed Contributor

Re: how to grep a file name out of list of files within time period

>I do something very similar to Bill

 

Not similar but much much better.  You only use one pass of find.

 

>explain $10 ~ /11 .../ what exactly its meaning?

 

Field $10 matches the specially tailored given ERE pattern, useless in the more general case.

Basically this is silly stuff trying to do pattern matching to solve a range check for between.

 

> [[ $(grep -c "mismatch" $MYFILE) -gt 0 ]] && echo "found it: $MYFILE"

 

Better to use grep -q directly:

grep -q "mismatch" $MYFILE && echo "found it: $MYFILE"

 

-q is also better since it stops when it finds the first match and not after reading the whole file!

 

>It should only pick up the latest date file

 

How do we determine "latest date file"?

Do we pick only the newest file and if that has  "mismatch"?

Or only check the N newest files?