You can use hashes in Bash.
#! /bin/bash
exec <<EOF
my_file_210804_2056_856.csv_20210804170806
my_file_210804_2056_856.csv_20211119181137
my_file_210805_2056_857.csv_20210805200847
my_file_210805_2056_857.csv_20211119181137
EOF
declare -A files
while read -r line; do
if [[ $line =~ (.*csv)_(.*) ]]; then
if [[ ${files[${BASH_REMATCH[1]}]} < ${BASH_REMATCH[2]} ]]; then
files[${BASH_REMATCH[1]}]=${BASH_REMATCH[2]}
fi
fi
done
for name in "${!files[@]}"; do
printf '%s_%s\n' "$name" "${files[$name]}"
done
declare -A
declares a hash. read -r line
reads a line. The while
loop does this until nothing is left. [[ $line =~ (.*csv)_(.*) ]]
compares the line read with a regular expression and captures the parts before and after the underline between the extension and the time stamp in the array BASH_REMATCH
. The if
condition checks, if the already stored value is lower then the new value. Non existing values are zero. If the value is small, the hash gets updated with the current value. The for
loop iterates over the hash and reconstructs the remaining file names.