Search on date range of file based on user input


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search on date range of file based on user input
# 1  
Old 03-30-2012
Question Search on date range of file based on user input

Hello I would like to ask for help with a script to search a directory that contains many log files and based on a users input after being prompted, they enter a date range down to the hour which searches the files that contain that range.

I dont know how to go about this. I am hoping that the user would be prompted by entering the hour to search but that wont work when the day changes and you have a number of files of separate days but same hour.

Would the user need to enter a variable for each date component "enter year, enter month, enter day, enter hour and then the script searches the best range of files
XSLog$YYYY.$MM.$dd-$hh.01.00.txt - if this is the best way how could this be done based on the log file format below?

Code:
#! /usr/bin/ksh
/var/logs/
ls -ltr 
XSLog2012.03.28-09.20.00.txt XSLog2012.03.28-09.32.00.txt
XSLog2012.03.28-09.49.00.txt XSLog2012.03.28-10.01.00.txt 
#prompt user for start date and time
echo you are about to search filename based time range of an hour"
echo Time year month day hour start = " read stime 
echo  Time year month day hour stop  = " read etime
# have left out rest of the action on the file that strips out data

I hope it makes sense what I am asking.
Thankyou

Last edited by lostincashe; 03-30-2012 at 06:09 AM..
# 2  
Old 03-30-2012
Got to this site,it might be use full..
shell - delete file - specific date - Stack Overflow
# 3  
Old 03-30-2012
Thanks for the site. That find command is very handy and powerful but I am not sure I can use it in my case.
Some problems I have are.
-That the directory contains upto 2 weeks of files.
-Can contain other file types and I only want it to search the XSLogxxxxx files.
-The user will type in a day and hour to search on.
-From that user input, the script then searches the small range of files based on date and hour for whatever data they need.

I was thinking of using the user input after being prompted for "year" "month" "day" and "hour" and using the variables of those to produce the file range to search on.
Code:
echo enter year: "
read YYYY
echo enter month: "
read MM
echo enter day: "
read DD
echo enter Hour: "
read HH
 sed -e '/./{H;$!d;}' -e 'x;/1234/!d'  XSLog$YYYY.$MM.$DD-$HH*

I have not tried this but would this be an approach ?
Thankyou for any advice better methods or examples
# 4  
Old 03-30-2012
I don't see why it wouldn't work.

There's one thing in your favor -- YYYY MM DD HH MM SS dates sort alphabetically. ls will give you them in date order. Then all you do is an alphabetic < > comparison to see if they're in the range you want...

Code:
printf "Enter start YYYY/MM/DD HH "
# Should work for YYYY/MM/DD HH or YYYY MM DD HH
IFS="/ " read S_YYYY S_MM S_DD S_HH

printf "Enter end YYYY/MM/DD HH "
IFS="/ " read E_YYYY E_MM E_DD E_HH

EARLIEST="XSLog$S_YYYY.$S_MM.$S_DD-$S_HH.00.00.txt"
LATEST="XSLog$E_YYYY.$E_MM.$E_DD-$E_HH.59.59.txt"

ls -1 | awk -v E="$EARLIEST" -v L="$LATEST" '($0 >= E) && ($0 <= L)' | xargs sed -e '/./{H;$!d;}' -e 'x;/1234/!d'

# 5  
Old 03-30-2012
Thanks very much, I will try this when I am next at work on Monday.
I was not aware of the IFS "/" so it was interesting to read your post.
Thanks againSmilie
# 6  
Old 03-30-2012
It was IFS="/ " not IFS="/" the space makes a difference. Without it, it will split only on /, not space.

read will split between variables on any character in IFS. If there's not enough variables, the last variable will get everything jammed into it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk command to search based on 5 user input fields

Field1=”” Field2=”” Field3=”” Field4=”” Field5=”” USER INPUT UP TO 5 FIELDS awk -F , '{ if ( $3 == Field1 && $6 == Field2 && $8 == Field3 && $9 == Field4 && $10 == Field5) print $0 }' /tmp/rodney.outD INPUT FILE (Rodney.outD): ... (3 Replies)
Discussion started by: rmerrird
3 Replies

2. Shell Programming and Scripting

Bash to search file based off user input then create new file

In the below bash a file is downloaded when the program is opened and then that file is searched based on user input and the result is written to a new file. For example, the bash is opened and the download.txt is downloaded, the user then enters the id (NA04520). The id is used to search... (5 Replies)
Discussion started by: cmccabe
5 Replies

3. UNIX for Dummies Questions & Answers

Find the count of files by last created date based on the given date range

My unix version is IBM AIX Version 6.1 I tried google my requirement and found the below answer, find . -newermt “2012-06-15 08:13" ! -newermt “2012-06-15 18:20" But newer command is not working in AIX version 6.1 unix I have given my requirement below: Input: atr files: ... (1 Reply)
Discussion started by: yuvaa27
1 Replies

4. UNIX for Advanced & Expert Users

Help with ksh script to list, then cp files from a user input date range

Hi, I'm quite new to ksh scripting, can someone help me with this. Requirements: I need to create a script that list the files from a user input date range. e. g. format of file: *c1*log.2012-12-22-14-00* *c1*log.2012-12-22-14-00* *c1*log.2012-12-22-14-00*... (1 Reply)
Discussion started by: chococrunch6
1 Replies

5. Shell Programming and Scripting

display Range of date depend on user input php

Hi, i am very new to php Is it possible to display Range of date depend on user input day example: user input 2 day start from 28/4/12 it will add 2 day from date of input so display should look like this 28/4/12 to 30/4/12 then from 30/412 user add another 4 date so will... (0 Replies)
Discussion started by: guidely
0 Replies

6. Shell Programming and Scripting

Search for a specific data in a file based on a date range

Hi, Currently I am working on a script to automate the process of converting the log file from binary into text format. To achieve this, partly I am depending on my application’s utility for this conversion and the rest I am relying on shell commands to search for directory, locate the file and... (5 Replies)
Discussion started by: svajhala
5 Replies

7. Shell Programming and Scripting

Append file based upon user input-- solved

Ok, I have a script with a commandline option that allows the user to add a custom function to the script file. I have tried everything in my limited knowledge of sed to get this to work and keep coming up short. I need sed to search for a line starting with a pattern, I've got that part so far,... (0 Replies)
Discussion started by: DC Slick
0 Replies

8. Shell Programming and Scripting

search file, change existing value based on input (awk help)

I have a file (status.file) of the form: valueA 3450 valueB -20 valueC -340 valueD 48 I am tailing a data.file, and need to search and modify a value in status.file...the tail is: tail -f data.file | awk '{ print $3, ($NF - $(NF-1)) }' which will produce lines that look like this: ... (3 Replies)
Discussion started by: nortonloaf
3 Replies

9. Shell Programming and Scripting

Report file extraction based on Date range

Hi all, Iam writing a script, which will extract all the files from Start_Date to End_Date. Files are date stamped as YYYYMMDD. For ex: Start_Date='20051001' End_Date='20060331' extract files such as........ ramp_20050810.rpt ramp_20050915.rpt ramp_20051001.rpt ramp_20051010.rpt... (2 Replies)
Discussion started by: ganapati
2 Replies

10. Shell Programming and Scripting

Log File date compare for user defined range

:confused: Hi i am a noob and need a little help to finish my shell script. I am learning as i go but hit a problem. I am search thorugh logs(*.rv) files to find entires between two user defined dates, The script so far looks for the "START" and "END" of each entry at sees if it belongs To... (0 Replies)
Discussion started by: mojo24
0 Replies
Login or Register to Ask a Question