Issue
I have a set of filenames (all of the same length) in a given dir and would like to find all the distinct substrings at a given position range in the filename (there will be many filenames with the same substring).
Specifically the substring I am interested starts at position 7 of the filename and goes 10 characters long
for file in *; do
if [ -d "$file" ]; then
file_basename=`basename $file`
substr=${file_basename:7:10}
done
I would like to write those unique substrings to either a file or a data structure that I can then loop through.
So the set of filenames
........12s456tyer..........
........12s436tyer..........
........12s456tyer..........
........12s436tyer..........
would lead to the 2 strings
12s456tyer
12s436tyer
Solution
If you use bash 4.0 or newer, you can create a unique associative array:
declare -A distinct_substrings=()
shopt -s nullglob # Prevent '*' from expanding to a literal '*'
for file in *; do
if [[ -f $file ]]; then
file_basename=${file##*/} # Not necessary if files are expanded from current dir.
substr=${file_basename:7:10}
distinct_substrings[$substr]=$substr
fi
done
# Do stuff with "${distinct_substrings[@]}"
Answered By - konsolebox Answer Checked By - Terry (WPSolving Volunteer)