Create tst.awk
file:
$ cat tst.awk
{if(NF=="7") printf "%s-%s - %02d - %s\n", substr($3,1,1), substr($5,1,1), $1, $7
if(NF=="9") printf "%s%s-%s%s - %02d - %s\n", substr($3,1,1), substr($4,1,1), substr($6,1,1), substr($7,1,1), $1, $9}
bash one-liner
$ for file in *Edu.txt ; do newfilename=`awk -f tst.awk <<<"$file"`; echo mv "$file" "$newfilename"; done
- Note that
echo
before mv
is to safely preview resulting command. Remove echo
to run actual mv
command.
awk.tst
is created to keep terminal screen clean and for later modifications. so in terminal you only run awk.tst
file in one-liner
instead of whole awk
code
- This works with the given sample file pattern. If there are any file that contains more than two words between
-
then simply add if
condition line in awk
file as NF=="#"
whereas #
is number of words between each space and edit printf
formatting accordingly.
Below is the list of what was there in current working directory before:
$ find .
.
./tst.awk
./01 - Some - Else - Edu.txt
./02 - Some thing - Else where - Edu.txt
./3 - SOME THING - ELSE WHERE - Edu.txt
Resulting Output
$ find .
.
./tst.awk
./S-E - 01 - Edu.txt
./St-Ew - 02 - Edu.txt
./ST-EW - 03 - Edu.txt
also see here for other methods AWK, SED, REGEX to rename files