Maybe not very smart solution:
Instead of echo-ing collect filenames inside an array ...
Just an idea ... example output:
NEW GROUP
[Hi8] Hi8-01-002.avi
NEW GROUP
[VHS] VHS-01-001.avi
[VHS] VHS-01-002.avi
[VHS] VHS-02-002.avi
NEW GROUP
[XZU] XZU
Edit 1:
based on Anthony Geoghegan 's answer avoid the pipes at the beginning of the loop and use bash globbing. Take a look at his comment.
Improved script:
last=""
for file in *avi; do
sub=${file:0:3}
[ "$last" != "$sub" ] && { echo "NEW GROUP"; last="$sub"; }
echo "[$sub] $file"
done
Edit 2:
as asked by @ Tony Tan in his third comment:
here you find a straigt forward solution to parse the collected file names to a function. There are many ways to do so. And I don't have much experience in bash scripting ... ;)
#!/bin/bash
SOURCE_DIR="$1"
cd "$SOURCE_DIR" || { echo "could not read dir '$SOURCE_DIR'"; exit 1; }
function parseFiles() {
echo "parsing files:"
echo "$1"
}
last=""
declare -a fileGroup
for file in *avi; do
# first 3 chars of filename
sub=${file:0:3}
if test -z "$last"; then
# last is empty. first loop
last="$sub"
elif test "$last" != "$sub"; then
# new file group detected, parse collected
parseFiles "${fileGroup[*]}"
# reset array
fileGroup=()
last="$sub"
fi
# append name to array
fileGroup[${#fileGroup[@]}]=$file
done
parseFiles "${fileGroup[*]}"
encode()
function for each group of files separately? – RomanPerekhrest May 09 '17 at 08:26