I am currently using UNIX bash, and I want to write shell script that allows me to correct the date entry by using information in a file's pathname.
Background: I previously had 200 files that were not placed in directories. After doing my own script, I have automatically found the first line in each file (YY/MM/DD) and placed each file within their year directory, followed by sub-directories of their month and day. For example, 2002/12/19 is stored in the directory of 2002, then a subdirectory of 12, and another subdirectory of 19. Inside each file there are four pieces of information. The DATE is always the first line. Example Pic912831 (File Name) 2005/02/23 Wednesday February Barcelona I know that there are many files that have not been stored in a directory due to the first line within the file being different to majority of others. I had a look in one of the files that was unable to be matched to a directory – and noticed the date was “2005-02-23”, whereas my matched files were “2004/05/14”. In this scenario I presume I will have to use
tr ‘-‘ ‘/’
What I require and HAVE to do:
- Identifying all incorrect/unmatched files
- Echo the correct first line to a temporary file (which I presume is the YEAR?)
- Append the last 3 lines of the existing file (Day, Month and Location).
- Move the temporary file in order to overwrite the existing file
Of course, I understand that it will be something like.
echo $directory > temp tail -3 $file >> temp mv temp $file
The file names do not need to be changed, I just need to identify those incorrect files and do the steps above. I think to keep it tidy, I would like the unmatched files to have the format of YY/MM/DD rather than YY-MM-DD. In summary: I want to be able to write a script that will correct the date entry of those unmatched files.