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

I want to change the filenames like

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 •

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
   nf=$(echo "$i"|awk ' BEGIN { FS="." }
   if($2 == "G3")
      # change field two
      # 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"

Report •
Related Solutions

Ask Question