0

I want to zip the files based on the file names which are dynamic. I have file called : ClientNames

CODE, Client Name
1111, ABC
1231, XYZ
1211, APT
1561, OPT

My file name is : MAA.TRD.1111.2016.20.09.csv (FILENAME.CODE.YEAR.DATE.MONTH.csv) I have multiple files with different CODES in a directory.

Everytime I need to check the CODE, fetch its Client Name from ClientNames file and zip the file with name - (for filename: MAA.TRD.1111.2016.20.09.csv zip file name : ABC.20162009.csv; MAA.TRD.1211.2016.20.09.csv zip file name should be : APT.20162009.csv and likewise )

I want to design a loop where it will take one by one file in and zip it, rename it.

My code is:

FILES=MAA.TRD*.csv

for file in ${FILES}
    do
    ls -lrt MAA.TRD*.csv | cut -d '.' -f 3 > $SCRIPT/LogfileCODE
    cd $SCRIPT
    DATE=`date`
    Filename=`grep -Fwf $LogfileCODE $ClientNames | cut -d ',' -f 2`
    ZIPFILENAME="${Filename}_${DATE}"         
    echo " Zipping of file is starting "
    zip -j ${ZIPFILENAME}-$DATE.zip $file           
done 

But my for loop is taking all the files together. Please help.

7
  • maybe you want to use $file in your ls -lr instead of repeating the wildcard/glob?
    – Jeff Schaller
    Commented Sep 20, 2016 at 19:50
  • I want to capture just a CODE (1111) in the LogfileCODE so I am doing ls -lr & then cut.
    – Pooja25
    Commented Sep 20, 2016 at 20:09
  • you goal is not completely clear. are you trying to zip one or possibly more files containing a CODE in the filename into a zip file with a name you set based on the CODE in the original filenames? or are you just renaming files (i.e. one at a time) and zipping the renamed file?
    – MikeA
    Commented Sep 20, 2016 at 20:34
  • I am sorry if my questions isn't clear. Please check again, I have updated.
    – Pooja25
    Commented Sep 20, 2016 at 20:43
  • I am trying to rename the file with the respective ClientNames & then trying to zip it.
    – Pooja25
    Commented Sep 20, 2016 at 20:44

1 Answer 1

0
1  DATE=$(date '+%Y%m%d')
2
3  for FILE in MAA.TRD*.csv
4  do
5      CODE=$(echo $FILE | cut -d. -f3)
6      CLIENT=$(grep -w $CODE ClientNames | sed 's/^.*, //')
7      NEWFILE=$CLIENT.$DATE.csv
8      mv $FILE $NEWFILE
9      echo " Zipping of $NEWFILE is starting "
10     zip -j $NEWFILE.zip $NEWFILE           
11 done

1 - set the date, best to use a format like this that won't have spaces in it, feel free to adjust to your preference

3 - just loop on the glob of files you have in the directory (this will expand to all files matching the naming convention you outline)

5 - get the CODE from the filename

6 - get the CLIENT from ClientNames based on the CODE of the current file (using sed here to include stripping the space after the comma, if there is not actually a space cut -d, -f2 will work instead) - this assumes ClientNames is in the CWD, use a full path if necessary

7 - build the new filename for easy reuse later

8 - rename the file to your new name

10 - zip the file (you don't want a zipfile ending in .csv)

2
  • this is creating just one zip file but I want to create zip file for each NEWFILE
    – Pooja25
    Commented Sep 21, 2016 at 18:28
  • did you try this code, and you are only getting one zip file? the loop acts individually on every file matching MAA.TRD*.csv in your directory, so you should end up with a new zip file for each different file it finds.
    – MikeA
    Commented Sep 21, 2016 at 18:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .