Home > Software design >  How to pack csv files having same prefix name into zip in bash
How to pack csv files having same prefix name into zip in bash

Time:11-25

Assume that I have many csv file located into /home/user/test

123_24112021_DONG.csv
122_24112021_DONG.csv
145_24112021_DONG.csv
123_24112021_FINA.csv
122_24112021_FINA.csv
145_24112021_FINA.csv
123_24112021_INDEM.csv
122_24112021_INDEM.csv
145_24112021_INDEM.csv

As you can see, all files have three unique prefix :

145
123
122

And, I need to create zip per prefix which will contains csv files. Note that in reality, I dont know the number of csv file, It is just an example (3 csv files per prefix). I developed a code that extract prefixes from all csv names in bash table :

for entry in "$search_dir"/*
do
  # extract csv files
  f1=${entry##*/}
  echo $f1
  # extract prefix of each file
  f2=${f1%%_*}
  echo $f2
  # add prefix in table
  liste_sirets =($f2)
done

# get uniq prefix in unique_sorted_list

unique_sorted_list=($(printf "%s\n" "${liste_sirets[@]}" | sort -u ))
echo $unique_sorted_list

which give the result :

145
123
122

Now I want to zip each three files defined by their prefix in same zip file : In other word, create 123_24112021_M2.zip which will contains

123_24112021_DONG.csv
123_24112021_FINA.csv
123_24112021_INDEM.csv

and 122_24112021_M2.zip 145_24112021_M2.zip ...

So, I developed a loop which will focus on each prefix name of csv files located in local path then zip all having the same prefix name :

for i in $unique_sorted_list
do
for j in "$search_dir"/*
do
if $(echo $j| cut -d'_' -f1)==$i
zip -jr $j
done

But, it does not work, any help, please ! thank you !

CodePudding user response:

Using bash and shell utilities:

#!/bin/bash

printf '%s\n' *_*.csv | cut -d_ -f1 | uniq |
while read -r prefix
do
        zip "$prefix".zip "$prefix"_*.csv
done

CodePudding user response:

Using bash 4 associative arrays:

# declare an associative array
declare -A unq

# store unique prefixes in array unq
for f in *_*.csv; do
   unq["${f%%_*}"]=1
done

# iterate through unq and create zip files
for i in "${!unq[@]}"; do
   zip "$i" "${i}_"*
done
  • Related