how to rename filenames by shifting words

Vmware Workstation 7 for windows and lin...
July 9, 2010 at 20:40:16
Specs: UNIX
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.txt

I want to change the filenames like
2000_01_MFM_input.txt
2000_02_MFM_input.txt
...,
...,
2010_04_MFM_input.txt

Thins 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.


See More: how to rename filenames by shifting words

Report •

#1
July 10, 2010 at 13:54:06

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



Report •
Related Solutions


Ask Question