1

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:

  1. Identifying all incorrect/unmatched files
  2. Echo the correct first line to a temporary file (which I presume is the YEAR?)
  3. Append the last 3 lines of the existing file (Day, Month and Location).
  4. 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.

  • 1
    Do you want to match on the file names? Please give examples of your source file names and the directories in which they should be placed. Do you want the files renamed too? If so please say so. – roaima Apr 22 at 15:58
  • 1
    @roaima I have edited my main post to answer your question. Thank you very much! – Ardenksie Apr 22 at 16:03
  • 2
    It's still unclear as to what your files look like, as "/" is an illegal character for filenames. Are the date strings you want to change only on the first line of the file, with a completely unrelated filename? – Egon Apr 22 at 16:19
  • 1
    @Egon I am so sorry, I was having a moment and was thinking of something else. I edited it now. So sorry! – Ardenksie Apr 22 at 16:37
  • 2
    In one sentence you say he script needs to convert 02 to February. In the next you seem to counter that. Please edit your question so that it's got a consistent set of requirements. And some examples. – roaima Apr 22 at 16:50
1

If you don't need any smarts about the date structure, i.e. it will always be of the form YYYY/MM/DD or YYYY-MM-DD, you can get away with a pretty simple command that will just correct the first line of every file in the entire directory tree.

find . -type f -exec sed -i '1 s:-:/:g' {} \;
  • sed -i edits the file in-place, no need to concatenate and move files around.
  • '1 ... edits only the first line of each file.
  • 's:-:/:g' replaces every instance of "-" on the line with "/"

After this you should be able to re-run your original script and have these files sorted into their correct date directories.

  • Hi Egon, that is very helpful, I understand what you are doing and it works. However, is there a method to do it in the steps mentioned in my post? I have to do it in that specific format. – Ardenksie Apr 22 at 19:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.