Wanted to rename file

Hello ,

I wanted to rename one file which is having name as....

ABCD_PQRSTUVW_XYZ_20140205193338_00172_1.DAT

TO

ABCD0000_PQRSTUVW_20140205193338.DAT

here..
1.20140205193338 is the timestamp of file creation which should not get change.

  1. XYZ should and be removed

  2. _00172_1 should removed [ this one is varying ]

any trick in a single line command.

try this :-

for loop in `ls ABCD_PQRSTUVW_XYZ_20140205193338_00172_1.DAT`
do
new_file=`echo $loop | awk -F "_" '{print $1"_"$2"_"$4".DAT"}'`
mv $loop $new_file
done

same code in single line

for loop in `ls ABCD_PQRSTUVW_XYZ_20140205193338_00172_1.DAT`; do new_file=`echo $loop | awk -F "_" '{print $1"_"$2"_"$4".DAT"}'`;mv $loop $new_file ; done

for multiple files with same name format :_

for loop in `ls *_*_XYZ_*_00172_1.DAT`
do
new_file=`echo $loop | awk -F "_" '{print $1"_"$2"_"$4".DAT"}'`
mv $loop $new_file
done

---------- Post updated at 10:45 AM ---------- Previous update was at 10:41 AM ----------

replace this line

awk -F "_" '{print $1"_"$2"_"$4".DAT"}'

with following

awk -F "_" '{print $1"0000_"$2"_"$4".DAT"}'
R00743392: ~/forum/20140331 > ls
ABCD_PQRSTUVW_20140205193338.DAT
ABCD0000_PQRSTUVW_20140205193338.DAT

Hello,

Here is a solution which may help you.

a=`echo "ABCD_PQRSTUVW_XYZ_20140205193338_00172_1.DAT" | awk -F"\_" '{match($0,/\....$/); a=substr($0,RSTART,RLENGTH);} {print $1 OFS $2 OFS $3 OFS $4 a}' OFS="_`
 
mv actual_file_name $a

Thanks,
R. Singh

If you are using a for loop, use the below code

mv ${i} $(awk '{split($0, a, "."); $1 = substr(($1 "00000000"), 1, 8); printf "%s_%s_%s.%s\n", $1, $2, $4, a[2]}' FS='_' <<< ${i})

else

mv ABCD_PQRSTUVW_XYZ_20140205193338_00172_1.DAT $(awk '{split($0, a, "."); $1 = substr(($1 "00000000"), 1, 8); printf "%s_%s_%s.%s\n", $1, $2, $4, a[2]}' FS='_' <<< 'ABCD_PQRSTUVW_XYZ_20140205193338_00172_1.DAT')

This isn't a one-liner, but (except for the mv command), it only uses shell (at least ksh and bash ) built-ins. If you save the following in a file named tester :

#!/bin/ksh
pattern='*_*_*_*_*_*.*'
for i in $pattern
do	if [ "$pattern" = "$i" ]
	then	printf "No files matching desired pattern found.\n" >&2
		exit 1
	fi
	suffix=${i##*.}
	nf6=${i%_*}
	nf5=${nf6%_*}
	f4=${nf5##*_}
	f1=${i%%_*}
	f2_4=${nf5#*_}
	f2=${f2_4%%_*}
	printf "Moving '%s' to '%s'\n" "$i" "${f1}0000_${f2}_${f4}.$suffix"
	mv "$i" "${f1}0000_${f2}_${f4}.$suffix"
done

and make it executable:

chmod +x tester

then, if you have the following files in your directory:

ABCD_PQRSTUVW_XYZ_20140205193338_00172_1.DAT
CDef_pQrStUvW_xyz_20140206121314_00174_2.c
EFGH_pqrstuvw_xyz_20140206121325_00175_3.lib
bcDE_PqRsTuVw_xyz_20140206121314_00173_1.pdf
orig
tester

and run the command:

./tester

it will print:

Moving 'ABCD_PQRSTUVW_XYZ_20140205193338_00172_1.DAT' to 'ABCD0000_PQRSTUVW_20140205193338.DAT'
Moving 'CDef_pQrStUvW_xyz_20140206121314_00174_2.c' to 'CDef0000_pQrStUvW_20140206121314.c'
Moving 'EFGH_pqrstuvw_xyz_20140206121325_00175_3.lib' to 'EFGH0000_pqrstuvw_20140206121325.lib'
Moving 'bcDE_PqRsTuVw_xyz_20140206121314_00173_1.pdf' to 'bcDE0000_PqRsTuVw_20140206121314.pdf'

and if you run it again, it will write:

No files matching desired pattern found.

to the standard error output and exit with exit code 1. An ls after running tester will show:

ABCD0000_PQRSTUVW_20140205193338.DAT
CDef0000_pQrStUvW_20140206121314.c
EFGH0000_pqrstuvw_20140206121325.lib
bcDE0000_PqRsTuVw_20140206121314.pdf
orig
tester
1 Like

Thanks a lot for all!
all code are working.