Files extraction - any help ?

Hi Friends,
i am new to unix,i have a big doubt/help.

I have files in folders SER1 and SER2 with naming convention as below
file_2010-03-19.txt and so on
the file naming format is file_<date>.txt. I would like to copy the files to directory "Landing"

I have entries in a log file log.txt as below

2010-02-19 SER1 SER2 failed
2010-02-10 SER2 failed
2010-04-14 SER1 failed
2010-04-26 SER1 SER2 failed

Its indicate that the files ,named using the date(first filed) not extracted(copied)successfully from the folders SER1 (2nd) and SER2 (3rd)

Now using this log file I want look into the failed folders and try to extract the files that named with date in1st filed of log file. And the script that I we are going to develop should use this log.txt file and extract all files from respective failed folders(SER1/SER2 or from Both).

If the file extraction once again failed for any date/SER1/SER2 , my script should prepare same log file (log.txt) at the end.
If the extraction success from both folders, no need of entry in log file. If any one one folder or both failed, I need a entry in th log file.

Can any one help me out on this?

You have and had several infractions cause of code tags by different mods; if you have 30 points, you are out. Writing this here in the thread since it seems you ignore PMs about it.

Your post is totally messed up with formatting tags (where there is no reason for it - there is not only the bold tags) - you'll have to replace them with code tags your own.

You can do something like that :

LogFile=log.txt
TargetDir=Landing

> ${LogFile}.new

while read date infos
do
   set -- ${infos}
   eval status=\$$#

   # Keeps entries with status not equal to failed

   if [ "${status}" != "failed" ]
   then
      echo "${date} ${infos}" >> ${LogFile}.new
      continue
   fi

   # Extract file in each failed directory

   new_failed_dirs=""
   while [ $# -gt 1 ]
   do
      dir=$1
      shift
      file="${dir}/file_${date}.txt"
      echo "\nExtracting file ${file} ..."
      if cp ${file} ${TargetDir}/
      then
          echo "Ok."
      else
          echo "Failed."
          new_failed_dirs="${new_failed_dirs} ${dir}"
      fi
   done

   # Write failed directory into new log

   if [ -n "${new_failed_dirs}" ]
   then
      echo "${date} ${new_failed_dirs} failed" >> ${LogFile}.new
   fi

done < log.txt
mv ${LogFile} ${LogFile}.old && mv ${LogFile}.new ${LogFile}

Input file log.txt :

2010-02-19 SER1 SER2 failed
2010-02-10 SER2 failed
2010-04-14 SER1 failed
2010-04-26 SER1 SER2 failed

Script output :


Extracting file SER1/file_2010-02-19.txt ...
cp: SER1/file_2010-02-19.txt: No such file or directory
Failed.

Extracting file SER2/file_2010-02-19.txt ...
Ok.

Extracting file SER2/file_2010-02-10.txt ...
cp: SER2/file_2010-02-10.txt: No such file or directory
Failed.

Extracting file SER1/file_2010-04-14.txt ...
cp: SER1/file_2010-04-14.txt: No such file or directory
Failed.

Extracting file SER1/file_2010-04-26.txt ...
Ok.

Extracting file SER2/file_2010-04-26.txt ...
cp: SER2/file_2010-04-26.txt: No such file or directory
Failed.

Files in Landing directory ;

file_2010-02-19.txt  file_2010-04-26.txt

New log file log.txt :

2010-02-19  SER1 failed
2010-02-10  SER2 failed
2010-04-14  SER1 failed
2010-04-26  SER2 failed

The old log file is saved in log.txt.old

Jean-Pierre.

Hi friend,

Thank you very much. Can you explain how this script working. Especially i dont understand the while loop.

while read date infos
whats s this infos. whay r u using it in while loop. whats the use. and

set -- ${infos} - i dont understand this

eval status=\$$# - i dont understand this

Please explain me Jean-Pierre.

read date infos

This command reads a line from the input file, and stores the first word in the variable date and the remaining in the variable infos.

set -- ${infos}
eval status=\$$#

This command parses the contents of the variable info,words are stored into variables $1 $2 $3... the number of words is given by $#
The value of the last word is stored in the variable status

$ echo "2010-02-19 SER1 SER2 failed" | read date infos
$ echo "date=[$date] infos=[$infos]"
date=[2010-02-19] infos=[SER1 SER2 failed]
$ set -- ${infos}
$ echo "\$#=$# \$1=[$1] \$2=[$2] \$3=[$3]"$#=3 $1=[SER1] $2=[SER2] $3=[failed]
$ eval status=\$$#
$ echo "status=[$status]"
status=[failed]
$ set -x
$ eval status=\$$#
+ eval status=$3
+ status=failed
$ 

Jean-Pierre.

thank you very much