Sponsored Content
Top Forums Shell Programming and Scripting Find Files with a input in directories Post 302729469 by migurus on Friday 9th of November 2012 08:05:52 PM
Old 11-09-2012
I understood the problem lays in the fact that a single file can span several days worth of data, such as

Quote:
Code:
sensor1_log_from_2012_07_01_10_:_30_to_2012_07_03_10_:_30

You need a method to find appropriate file for a given date, if I am not mistaken.

I propose to use gawk capacity to manipulate dates and use unix epoch date form, which can be compared arithmetically.

1) convert given date/time to epoch form
2) go through files and for each extract start_time and end_time from the file name and using a mktime function convert both into the unix epoch time
3) if given_time >= start_time and given_time <= end_time then you got the file needed.

Note: I'm not sure if awk/nawk has mktime function, that is why emphasis on gawk.

To demonstrate I put a simple script:

Code:
#!/bin/ksh
s="sensor1_log_from_2012_07_01_10_:_30_to_2012_07_03_10_:_30"
echo $s|
gawk '
BEGIN{
        qry1_time = mktime("2012 06 20 18 30 00");
        qry2_time = mktime("2012 07 02 10 00 00");
        qry3_time = mktime("2012 07 03 22 45 00");
}
{
n=split($0, arr, "_");
str_fr_time = arr[4]" "arr[5]" "arr[6]" "arr[7]" "arr[9]" 00"
str_to_time = arr[11]" "arr[12]" "arr[13]" "arr[14]" "arr[16]" 00"
fr_time = mktime(str_fr_time);
to_time = mktime(str_to_time);
print "FR:"str_fr_time
print "TO:"str_to_time
if(qry1_time >= fr_time && qry1_time <= to_time)  
        print "OK for " strftime("%x %X", qry1_time);
else 
        print "NO for " strftime("%x %X", qry1_time);
 
if(qry2_time >= fr_time && qry2_time <= to_time)  
        print "OK for " strftime("%x %X", qry2_time);
else 
        print "NO for " strftime("%x %X", qry2_time);
if(qry3_time >= fr_time && qry3_time <= to_time)  
        print "OK for " strftime("%x %X", qry3_time);
else 
        print "NO for " strftime("%x %X", qry3_time);
 
}'

And here is the output
Code:
 
FR:2012 07 01 10 30 00
TO:2012 07 03 10 30 00
NO for 06/20/12 18:30:00
OK for 07/02/12 10:00:00
NO for 07/03/12 22:45:00

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find files in Directories

Hello, We have in a file a list of the path of files from one server. We want to search if these files exist on another server , but the path on this new server isn't the same. We want to use the command "awk" but there isn't the god way. Example: on server 1 in a file : listServer1.txt... (4 Replies)
Discussion started by: steiner
4 Replies

2. UNIX for Advanced & Expert Users

find -type d returning files as well as directories

Can anyone see why the following command returns all files and not just the directories as specified? find . -type d -exec ls -F {} \; Also tried find . -type d -name "*" -exec ls -F {} \; find . -type d -name "*" -exec ls -F '{}' \; -print Always returns all files :-\ OS is... (2 Replies)
Discussion started by: tuns99
2 Replies

3. Homework & Coursework Questions

Find and delete empty files and directories

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Need to make a script, to remove all empty files and folders from current category. It also should show the name... (2 Replies)
Discussion started by: Itixop
2 Replies

4. Shell Programming and Scripting

Script to delete files with an input for directories and an input for path/file

Hello, I'm trying to figure out how best to approach this script, and I have very little experience, so I could use all the help I can get. :wall: I regularly need to delete files from many directories. A file with the same name may exist any number of times in different subdirectories.... (3 Replies)
Discussion started by: *ShadowCat*
3 Replies

5. Shell Programming and Scripting

Script to go Into Directories and Find/Delete files

I have a task, I usually do manually, but with growing responsibilities I tend to forget to do this weekly, I want to write a script that automates this, but I cant seem to work it out in my head, I have the shell of it out, but need help, and you guys have helped me with EVERY problem I have... (5 Replies)
Discussion started by: gkelly1117
5 Replies

6. Shell Programming and Scripting

Find only files/directories with different permissions/owners

My git post-update has the following lines in it to make sure the permissions are set right: find /usr/local/apache/htdocs -type d -print0 | xargs -0 chmod 755 find /usr/local/apache/htdocs -type f -print0 | xargs -0 chmod 644 chown -R apache:apache /usr/local/apache/htdocsThe only problem is... (5 Replies)
Discussion started by: dheian
5 Replies

7. UNIX for Advanced & Expert Users

Find all files in the current directory excluding hidden files and directories

Find all files in the current directory only excluding hidden directories and files. For the below command, though it's not deleting hidden files.. it is traversing through the hidden directories and listing normal which should be avoided. `find . \( ! -name ".*" -prune \) -mtime +${n_days}... (7 Replies)
Discussion started by: ksailesh1
7 Replies

8. UNIX for Dummies Questions & Answers

Find Files In A List with known Partial Directories

First I'm new to Linux and have used the find command pretty often but this is where I've hit a snag. I have a file that contains 3500 files that I want to find and then eventually copy to my own directory (these files are all on a shared directory at work atm). Our work computer are huge and... (2 Replies)
Discussion started by: Myrona
2 Replies

9. UNIX for Dummies Questions & Answers

Loop over certain user directories and find files

Hello I have user directories that contain /temp directory. Example folders: /user1/temp/ /user2/temp/ /user3/temp/ How can i loop over all user directories and find all files only in their /temp folder? Thanks a lot for help! (3 Replies)
Discussion started by: flavius42
3 Replies

10. Shell Programming and Scripting

Find common files between two directories

I have two directories Dir 1 /home/sid/release1 Dir 2 /home/sid/release2 I want to find the common files between the two directories Dir 1 files /home/sid/release1>ls -lrt total 16 -rw-r--r-- 1 sid cool 0 Jun 19 12:53 File123 -rw-r--r-- 1 sid cool 0 Jun 19 12:53... (5 Replies)
Discussion started by: sidnow
5 Replies
GETDATE(1)							   User Commands							GETDATE(1)

NAME
getdate - AME SYNOPSIS
[-dv][-n dec][-f format] [ra dec sys] itype2otype [date and/or time] DESCRIPTION
Convert date and time between various formats [-dv][-n dec][-f format] itype2otype @file itype: nfd=ISOFITS fd=FITS, dt=yyyy.mmdd, hr=hh:mm:ss, deg=dd:mm:ss jd=Julian Date, mjd=Modified Julian Date hjd=Heliocentric Julian Date, mhjd=Modified HJD ep=epoch, epj=Julian epoch, epb=Besselian epoch lt=local time, ut=UT, ts=seconds since 1950-01-01 now=current time, ang=fractional degrees otype: fd=FITS, dt=yyyy.mmdd, jd=Julian Date, mjd=Modified Julian Date hjd=Heliocentric Julian Date, mhjd=Modified HJD hr=hh:mm:ss, deg=dd:mm:ss, ang=fractional degrees ep=epoch, epj=Julian epoch, epb=Besselian epoch ts=seconds since 1950-01-01, tsu=Unix sec, tsi=IRAF sec gst=Greenwich Sidereal Time, lst=Local Sidereal Time @file: First one or two columns are in itype format ra dec sys: Need for Heliocentric conversions -a: Append date to input file, if there is one -d: Print date without time -e: Print output as ET/TDT/TT converting from UT -f: Format for output number (C printf) -h hours: Longitude in hours, west positive -l degrees: Longitude in degrees, west positive -n: Number of decimal places in sec, epoch, JD -t: Print time without date -v: Verbose getdate 3.8.4 June 2012 GETDATE(1)
All times are GMT -4. The time now is 11:40 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy