Follow

Follow
Bash Script to categorize files into batches

Photo by Gabriel Heinzer on Unsplash

Bash Script to categorize files into batches

Syed Jafer K's photo
Syed Jafer K
·Oct 4, 2022·

1 min read

Play this article

Consider we are having this set of files,

image.png


#!/bin/sh
declare -A dictionary
FILES=`ls *.xml`
for FILE in ${FILES}
do
  IFS='_' read -r -a array <<< "$FILE"
  if [ ! -v dictionary[${array[1]}${array[3]}] ];
  then
    dictionary[${array[1]}${array[3]}]=$FILE
  else
    dictionary[${array[1]}${array[3]}]+=","$FILE
  fi
done

index=1
for key in "${!dictionary[@]}"; do
    echo "Batch $index - ${dictionary[$key]}"
    index=$(($index + 1))
done

Output:

image.png

 
Share this