0

I receive files with names constructed in the following format

[2letters e.g.AF][6-digit-number sequence][Date in ccyymmdd][Time in hhmmss]

For Example:

AF00010720120917144500.csv

I want to automate loading such files onto my database using the date part of the file.

something which may start like this:

#!/bin/bash
filename_datepart=$(echo `date -d "1 day ago" +"%d%m%Y"`)
filename="/home/hlosi/AF000107"$filename_datepart".csv"

But remember, the part 000107 changes with each new file.

2
  • How many files do you expect per day? Also, wouldn't you need the Time in hhmmsss? Commented Sep 17, 2012 at 12:58
  • I receive one file per day. I have tried reasoning with the data source about excluding the time part since it's only one file a day to no avail. Commented Sep 17, 2012 at 13:01

4 Answers 4

1

You can use wildcards to fill in the unknown values

#!/bin/bash
file=/home/hlosi/AF??????`date -d "1 day ago" +"%d%m%Y"`??????.csv
echo $file
Sign up to request clarification or add additional context in comments.

1 Comment

This worked fine: file=/home/hlosi/AF*date -d "1 day ago" +"%d%m%Y"*.csv
1

Here is a BASH solution:

#!/bin/bash

#The full name
fullname="/home/hlosi/AF00010720120917144500.csv"

#Rip off the directory
file=$(basename "$fullname")

#Now pull out just the characters that we want
extract=$(echo "$file" | cut -c3-8)
echo "You want: $extract"

5 Comments

Thanks, I have adapted this one to part of my problem. I will try it and get back to you. Thanks a lot.
Bash has built-ins so you don't need cut either, but you have to do it in stages; cuttail=${file#????????}; cutc8=${file%$cuttail}; echo "${cutc8#??}". Note also the use of double quotes when you interpolate a variable, except in assignments.
@tripleee -- I learned shell scripting before bash was common -- I tend to stick the old ways. You are right about the quotes though, I should have put those in.
Actually the string substitutions go pretty far back; they are certainly not Bash-only.
Since the date part is at a fixed location, ${file:8:8} is one-step solution in bash (although not POSIX).
0

I think you want this. In case you have to handle multiple files.

#!/bin/bash
fpath=/home/hlosi/
filename_datepart=$(echo `date -d "1 day ago" +"%d%m%Y"`)
files=$(find $fpath -iname "*$filename_datepart.csv")
for file in $files
do
    echo "found file: " $file
done

1 Comment

Especially since these people take days to remove old files, my script may have to choose the correct file using the date part.
0

forgive me for my ignorance there is -atime -ctime -mtime I think its -ctime

find  -ctime 1  -name \*.csv -print

-mtime match ending of csv and are exactly 1 day old, the trouble of this is it works in 24 hour period so files less than 24 hours but still yesterday would not show

this would be a simpler way of doing things since and would not care about changes in file name formatting for future proofing.

cd pathtcsv;
d=`date -d "1 day ago" +"%d"`
d=$d find . -type f -name \*.csv -ctime 1 -exec ls -l {} \;|awk '$7 ~ d'|awk '{print $NF}'|  awk '{ print substr( $0, length($0) - 1, length($0) ) }'

# D = $d which is set as yesterday's date, it finds files from yseterday that have csv ending it then does an ls, pipes into awk and checks out value 7 against the date of yesterday which $7 is the date value on ls -l, it finally prints the last field and pipes into a final awk which prints the string and splits from char position to char position which is what you wanted ? you need to figure out what chars you need here is another example of above for char positions of 0 to 10.

 d=$d find . -type f  -ctime 1 -name \*.csv -exec ls -l {} \;|awk '$7 ~ d'|awk '{print $NF}'|  awk '{ print substr( $0, 0, 10)}'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.