Pure awk
solution: basic idea is to extract last 3 chars of the basename, that means a substring form length - 6
(6 because need to account for the .txt
extension) to length - 3
. Here I merely extract that substring into VAR first, then create the command complete with mv
and two filenames to be ran; finally I ask system
function to run the command.
Code:
awk 'BEGIN{
VAR=sprintf("%s",substr(ARGV[1],length(ARGV[1])-6,3));
CMD=("mv "ARGV[1]" "VAR"_"ARGV[1]);
print CMD;
system(CMD);
close(CMD)
}' filename_1234567.txt
Demo:
$ awk 'BEGIN{VAR=sprintf("%s",substr(ARGV[1],length(ARGV[1])-6,3)); CMD=("mv "ARGV[1]" "VAR"_"ARGV[1]); print CMD; system(CMD); close(CMD)}' filename_1234567.txt
mv filename_1234567.txt 567_filename_1234567.txt
$ ls filename_1234567.txt
ls: cannot access filename_1234567.txt: No such file or directory
$ ls 567_filename_1234567.txt
567_filename_1234567.txt
$