Hello everybody, I have some files in directory. I want to shift 3 characters of filenames to the right at a same time.
for example, I have filenames like $ls -l
01_2000.G3.input.txt
02_2000.G3.input.txt
...,
...,
04_2010.G3.input.txtI want to change the filenames like
2000_01_MFM_input.txt
2000_02_MFM_input.txt
...,
...,
2010_04_MFM_input.txtThins means I want to shift first 3 characters to the 8th position and I want to replace .G3. to _MFM_ also.
Any suggestions are welcome.
for each file name ending in G3.input.txt, call an awk script that.
if the 2nd field equals G3 change field 2 to MFM. split field 1 into 2 pieces
delimited by the underscore. Finally, build the string per spec.Make sure you are in the correct directory before executing the script. Change the cp to mv when you are sure the script works to your spec.
# cd <your directory> for i in *.G3.input.txt do nf=$(echo "$i"|awk ' BEGIN { FS="." } { if($2 == "G3") { # change field two $2="MFM" # split field one into two parts split($1, a, "_") printf("%s_%s_%s.%s\n", a[2], a[1], $2, $3) } } ' ) # rename the file cp "$i" "$nf" done
Yes (14) | ![]() | |
No (14) | ![]() | |
I don't know (15) | ![]() |