Следующий сценарий оболочки выполнит (большую часть) того, что вам нужно. Он не изменит исходный файл (не добавит "номер") к нему) - только вновь созданные файлы.
Надеюсь, комментарии достаточно ясны.
Использование несколько запутанного expr
вместо расширения параметра bash должно сделать его более переносимым:
#!/bin/sh
orig=ascdrg3.txt # start with this file
in=$orig
count=1 #loop variable
max=5 #number of files to create
while test "$count" -le "$max" ; do
# Remove extension
base=$(basename "$in" .txt)
# get the prefix
prefix=$(expr substr "$base" 1 $((${#base}-1)))
# get last letter
last=$(expr substr "$base" ${#base} 1)
while true ;
do
# Advance letter, while the file doesn't exist
last=$(echo "$last" | tr A-Z B-ZA)
last=$(echo "$last" | tr a-z b-za)
last=$(echo "$last" | tr 0-9 1-90)
# construct new file name
new="$prefix$last.txt"
# continue if it doesn't exist
# (otherwise, advance the last letter and try again)
test -e "$new" || break
test "$new" = "$orig" \
&& { echo "error: looped back to original file" >&2 ; exit 1; }
done
# Create new file
cp "$orig" "$new"
# Modify first line of new file
sed -i "1s/\$/number($count,$max)/" "$new"
# Advance counter
count=$((count+1))
# loop again
in=$new
done
Не требуется printf
-назначить новую переменную с помощью «расширения параметра»:
NEW=${VAR#*$PATTERN}
echo $NEW
script text and real script text.