Program to search for the files with specific format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Program to search for the files with specific format
# 1  
Old 03-07-2017
Program to search for the files with specific format

Hi All,

Your help on the below requirement is highly appreciated.
I am building a program which should pick the file from a source directory and place them into target directory. While selecting the file in source it should look for some pattern in the file name.

Ex:
File Name 1: ABC_I_05032017140330
File Name 2: ABC_I_07032017140340
File Name 3: ABC_I_07032017140312

All the above files are stored under same directory. My search should be like File name which starts with 'ABC' and File name contains '_I_' and the last part of the file is time stamp, My program have parameter to select the date range. so the search criteria should be like within that range, ex : Files which created between 07032017140340 and 07032017140312.

I tried to do with find command but could not get any idea like how to proceed with all the logic with loop. I do not want to loop all the files as we will not archive this source direcotory

Code:
echo "Program Begin" 

for file in `find . -type f -name "*_I_*`
do
 echo "Inside Loop" $file
 # Process file
 if [ -f "$file" ]; then
 echo "File Name=" $file 
 fi
done

echo "Program End"

Please help.

Thanks
Bharat
# 2  
Old 03-07-2017
Create two dummy files: dummy1 and dummy2, with filetimes you want
Code:
#  syntax touch -t yyyymmddhhmm.ss
touch -t  201707031403.12 dummy1
touch -t  201707031403.40 dummy2

Now create a find command using filetimes and the other criteria
Code:
find . -type f -name "*_I_* -a \( -newer dummy1 -a ! -newer dummy2  \) -exec ls -l {} \;

Run that and see if it gave you what you want. You may have to tweak the time statements in touch When it looks good put it back in your loop.
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 03-07-2017
Hi Jim,

Thanks a lot for your reply.
In my case timestamp is already in the file name. so i just need to select the files which meets the date range. Lets say i have below files.

f1_1
f1_2
f1_3
f1_4
f1_5

and if my input is 2 and 4. I should get the files 2,3 and 4. Something like between clause but with file name.

Thanks
Bharat
# 4  
Old 03-07-2017
Will your range ever go over midnight or stretch over more than one day?
This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 03-07-2017
To properly compare these filename we should change the DDMMYYYYHHMMSS format to YYYYMMDDHHMMSS - I have written a function ymd that I use below to compare filesnames to the supplied ranges:

Code:
echo "Program Begin"

from=07032017140340
to=07032017140312


# Convert DDMMYYYYHHMMSS to YYYYMMDDHHMMSS
ymd() {
    d=${1%????????????}
    m=${1#??}
    y=${1#????}
    m=${m%??????????}
    y=${y%??????}
    printf "%s" "$y$m$d${1#????????}"
}

from=$(ymd "$from")
to=$(ymd "$to")

find . -type f -name "ABC*_I_*" -print | while read file
do
    [ "$(ymd "${file##*_}") \> "$from" ] && continue
    [ "$(ymd "${file##*_}") \< "$to" ] && continue
    echo "File Name=" $file
done

echo "Program End"


Last edited by Chubler_XL; 03-07-2017 at 10:29 PM..
This User Gave Thanks to Chubler_XL For This Post:
# 6  
Old 03-08-2017
Hi Chubler,

Thanks for your reply.
I have the flexibility of change the DDMMYYYYHHMMSS format to YYYYMMDDHHMMSS. And yes it makes sense. I will make this change. But in the below code is there any way we can loop the date range first and then loop for the other condition, So most of the file get filters in first loop only.

Code:
find . -type f -name "ABC*_I_*" -print | while read file
do
    [ "$(ymd "${file##*_}") \> "$from" ] && continue
    [ "$(ymd "${file##*_}") \< "$to" ] && continue
    echo "File Name=" $file
done

I am not sure if unix stores any file creation date in server(Not the actual file creation date), even if i can filter using that date range also..it will be ok

Please help.

Thanks
Bharat
# 7  
Old 03-08-2017
The standards don't have a file creation date in the stat structure, but some filesystem types do store that data. IOf your files are on are a filesystem that stores a creation timestamp AND your operating system's code supporting that filesystem type maintains that data, the find utility on your operating probably has a primary like -atime, -ctime, and -mtime for creation dates.

If the find man page on your system doesn't have a primary to check creation timestamp or your files are stored on a filesystem type that doesn't maintain that data AND you haven't changed the mode of your files since you created them AND you wrote all of the data into them that they currently contain immediately after creating the files, then find's -ctime primary is probably sufficient for your needs. (The -ctime primary would be showing you the time at which data was last written into the file.)
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Running program and output files in specific directories

I have been running a program mseed2sac using the following command cd IV find . -type f -exec /swadmin/mseed2sac '{}' \; The problem is that I end up with a lot of files in directory IV. Instead I would like to select the designator HHZ, create a directory IV.SAC and all the files output... (11 Replies)
Discussion started by: kristinu
11 Replies

2. Shell Programming and Scripting

To find files having filename containing specific date format

Hi, I have a requirement to create a shell script(tcsh) that finds all the files in a directory having the file name containing date format "YYYYMMDDHHMM" and extract the date time part ""YYYYMMDDHHMM" for further processing. Could you please have any idea on this. trades_201604040000.out... (6 Replies)
Discussion started by: gopal.biswal
6 Replies

3. UNIX for Dummies Questions & Answers

Printing files in a specific format

Hi, I have following files in a directory with '.meta' extension, which have data in follwoing patterns. i need to print data from these files in below metioned format. please provide a script for this solution. file names: TEST_HISTORY_MTH.meta AB_TEST_1.meta cat... (2 Replies)
Discussion started by: AAHinka
2 Replies

4. Shell Programming and Scripting

search for content in files. Name of files is in another file. Format as report.

Hi I have multiple files in a folder and one file which contains a list of files (one on each line). I was to search for a string only within these files and not the whole folder. I need the output to be in the form File1<tab>string instance 2<tab> string instance 2<tab>string instance 3... (6 Replies)
Discussion started by: pkabali
6 Replies

5. UNIX for Dummies Questions & Answers

How to search all the files in a directory for a specific string

Hi Guys, I want to search the content of all the files (of a particular type like .txt) in a directory for a specific string pattern. Can anyone help me? Thanks (7 Replies)
Discussion started by: mwrg
7 Replies

6. Shell Programming and Scripting

search files without a specific text

Hi all, I need UNIX command that would give me all files without the string "4R" anywhere in the file. I have about a hundred files in my directory, and I need to list all files wihtout the string "4R" anywhere. Can anyone help me please? Thank you.l (4 Replies)
Discussion started by: risk_sly
4 Replies

7. Shell Programming and Scripting

Help make a program in cshell that searches and prints in a specific format

say I have a file named phones in that file every line is like that lastname^firstname^phone how can I make a program in cshell that searches for a specific string in phones and echos the result (if found) like that: lastname1 firstname1 phone1 ------------------ lastname2 firstname2... (8 Replies)
Discussion started by: h4wk
8 Replies

8. Shell Programming and Scripting

Search files that all contain 4 specific words

Hello, I want an one line command that brings me back all the files in a folder that contain 4 specific words anywhere inside them. I want to use find,xargs and grep. for example i know for one word the command would be: find . | xargs grep 'Word1' But i don't know for 4 specific words... (13 Replies)
Discussion started by: WoodenSword
13 Replies

9. UNIX for Dummies Questions & Answers

How to search files containing a specific word in the content

Hi all, Lets say I have 3 files a.txt and b.txt and c.txt. a.txt has the following text ==================== apple is good for health b.txt has the following text ==================== apple is pomme in french c.txt has the following text ==================== orange has citric acid... (1 Reply)
Discussion started by: amjath78
1 Replies

10. UNIX for Dummies Questions & Answers

Search all files for specific string

Hi Friends, How can I search all files in all slices on a unix system for a particular string within the file. e.g search string 'oracle' Thanks (4 Replies)
Discussion started by: sureshy
4 Replies
Login or Register to Ask a Question