简体   繁体   中英

Bash script to rename and zipping multiple files from text input

Currently i am making a zipping operation via simple bash script by using parametrized way of doing, it is taking three parameters:

#!/bin/bash
#$1 Create zip output directory if not exists
#$2 Go to directory of files that you need to make zip and read input file list
#$3 Zip content name, to write the output as expected filename
mkdir -p $1
cd $2 && cat filelist.txt | zip -@ $1/$3

It is doing the job fine and as expected, but i want to improve it by using fourth parameter, if there is a fourth and fifth parameter given to script eg $4 value is "ru-BUCAT" and $5 value is "ru-XB", assume that filelist.txt contains values :

VIB_ru-BUCAT_A01_D_CIB36P00.xml
VIB_ru-BUCAT_A01_D_CIR30P00.xml
VIB_ru-BUCAT_A01_D_CSG656RB1.xml
VIB_ru-BUCAT_A01_D_HBA23B450E.xml
VIB_ru-BUCAT_A01_D_HBA43T420.xml
VIB_ru-BUCAT_A01_D_HBC86K751S.xml
VIB_ru-BUCAT_A01_D_HBN880751.xml
VIB_ru-BUCAT_A01_D_HCA422120Q.xml
VIB_ru-BUCAT_A01_D_HCA643220Q.xml
VIB_ru-BUCAT_A01_D_HEZ338250.xml
VIB_ru-BUCAT_A01_D_HEZ394300.xml

I need to create copy of these files, contains same content, by changing substring of filename $4 "ru-BUCAT" and $5 "ru-XB":

VIB_ru-XB_A01_D_CIB36P00.xml
VIB_ru-XB_A01_D_CIR30P00.xml
VIB_ru-XB_A01_D_CSG656RB1.xml
VIB_ru-XB_A01_D_HBA23B450E.xml
VIB_ru-XB_A01_D_HBA43T420.xml
VIB_ru-XB_A01_D_HBC86K751S.xml
VIB_ru-XB_A01_D_HBN880751.xml
VIB_ru-XB_A01_D_HCA422120Q.xml
VIB_ru-XB_A01_D_HCA643220Q.xml
VIB_ru-XB_A01_D_HEZ338250.xml
VIB_ru-XB_A01_D_HEZ394300.xml

Then create zip file by using these files, after zip is created i don't need the files again the copied files again, so only necessity is to put files into zipped file.

Business logic defined like this, so please don't ask why i am doing this kind of operation.

Any helps would be appreciated, i tried somethings but i am quite new to bash so can't find good, readable solution for that.

Many thanks in advance!

Well, let's keep it simple. Parse each file to zip and make a copy with the right name in a dedicated directory before zipping.

#!/bin/bash
#$1 Create zip output directory if not exists
#$2 Go to directory of files that you need to make zip and read input file list
#$3 Zip content name, to write the output as expected filename
#$4 RegExp pattern looked for in filenames
#$5 Replace string to use instead of the pattern
mkdir -p $1
cd $2

# Create a temp dir to contains the zip file content
TMP_DIR=tmp_content
mkdir $TMP_DIR

# Parse all the files in filelist and copy them to the temp directory (with the new wname if a match is encountered)
for FILE in $( cat filelist.txt ); do
        NEW_FILE=$(sed -r "s/$4/$5/" <<< $FILE)
        cp -p $FILE $TMP_DIR/$NEW_FILE
done
cd $TMP_DIR
ls -1 | zip -@ $1/$3
rm -rf $TMP_DIR

You can then call your script this way:

$ ./my_script.sh /tmp/so_1/outpd so_1_cnt res.zip ru-BUCAT ru-XB

And with a list like this:

VIB_ru-Bdfdf_A01_D_HEZ394300.xml
VIB_ru-BUCAT_A01_D_CIB36P00.xml
VIB_ru-BUCAT_A01_D_CIR30P00.xml
VIB_ru-BUCAT_A01_D_CSG656RB1.xml
VIB_ru-BUCAT_A01_D_HBA23B450E.xml
VIB_ru-BUCAT_A01_D_HBA43T420.xml
VIB_ru-BUCAT_A01_D_HBC86K751S.xml
VIB_ru-BUCAT_A01_D_HBN880751.xml
VIB_ru-BUCAT_A01_D_HCA422120Q.xml
VIB_ru-BUCAT_A01_D_HCA643220Q.xml
VIB_ru-BUCAT_A01_D_HEZ338250.xml

gives the following zip content:

  adding: VIB_ru-Bdfdf_A01_D_HEZ394300.xml (stored 0%)
  adding: VIB_ru-XB_A01_D_CIB36P00.xml (stored 0%)
  adding: VIB_ru-XB_A01_D_CIR30P00.xml (stored 0%)
  adding: VIB_ru-XB_A01_D_CSG656RB1.xml (stored 0%)
  adding: VIB_ru-XB_A01_D_HBA23B450E.xml (stored 0%)
  adding: VIB_ru-XB_A01_D_HBA43T420.xml (stored 0%)
  adding: VIB_ru-XB_A01_D_HBC86K751S.xml (stored 0%)
  adding: VIB_ru-XB_A01_D_HBN880751.xml (stored 0%)
  adding: VIB_ru-XB_A01_D_HCA422120Q.xml (stored 0%)
  adding: VIB_ru-XB_A01_D_HCA643220Q.xml (stored 0%)
  adding: VIB_ru-XB_A01_D_HEZ338250.xml (stored 0%)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM