Wanted to rename file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Wanted to rename file
# 1  
Old 03-31-2014
[Solved] Wanted to rename file

Hello ,

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

Code:
ABCD_PQRSTUVW_XYZ_20140205193338_00172_1.DAT

TO
Code:
ABCD0000_PQRSTUVW_20140205193338.DAT

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

2. XYZ should and be removed

3. _00172_1 should removed [ this one is varying ]

any trick in a single line command.

Last edited by Scrutinizer; 03-31-2014 at 01:41 AM..
# 2  
Old 03-31-2014
try this :-
Code:
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

Code:
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 :_
Code:
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
Code:
awk -F "_" '{print $1"_"$2"_"$4".DAT"}'

with following
Code:
awk -F "_" '{print $1"0000_"$2"_"$4".DAT"}'

Code:
R00743392: ~/forum/20140331 > ls
ABCD_PQRSTUVW_20140205193338.DAT
ABCD0000_PQRSTUVW_20140205193338.DAT

Moderator's Comments:
Mod Comment Please use CODE (not ICODE) tags when displaying long lines of code and multi-line code and data samples. Note also that sample code and data goes between the tags; not after the tags.

Last edited by Don Cragun; 03-31-2014 at 02:17 AM.. Reason: Change ICODE to CODE tags and fix tag positioning.
# 3  
Old 03-31-2014
Hello,

Here is a solution which may help you.

Code:
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
# 4  
Old 03-31-2014
If you are using a for loop, use the below code
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
Code:
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')


Last edited by SriniShoo; 03-31-2014 at 03:57 AM.. Reason: format
# 5  
Old 03-31-2014
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:
Code:
#!/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:
Code:
chmod +x tester

then, if you have the following files in your directory:
Code:
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:
Code:
./tester

it will print:
Code:
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:
Code:
No files matching desired pattern found.

to the standard error output and exit with exit code 1. An ls after running tester will show:
Code:
ABCD0000_PQRSTUVW_20140205193338.DAT
CDef0000_pQrStUvW_20140206121314.c
EFGH0000_pqrstuvw_20140206121325.lib
bcDE0000_PqRsTuVw_20140206121314.pdf
orig
tester

This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 03-31-2014
Thanks a lot for all!
all code are working.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to copy file 3 times and rename based on another file

In the below bash I am trying to copy the only text file (always only one) in /home/cmccabe/Desktop/list/QC/metrics.txt and rename each of the 3 text files according to /home/cmccabe/Desktop/test/list.txt using lines 3, 4 ,5. This format (that is list.txt) is always 5 lines. Thank you :). ... (12 Replies)
Discussion started by: cmccabe
12 Replies

2. Shell Programming and Scripting

Rename specific file extension in directory with match to another file in bash

I have a specific set (all ending with .bam) of downloaded files in a directory /home/cmccabe/Desktop/NGS/API/2-15-2016. What I am trying to do is use a match to $2 in name to rename the downloaded files. To make things a more involved the date of the folder is unique and in the header of name... (1 Reply)
Discussion started by: cmccabe
1 Replies

3. UNIX for Dummies Questions & Answers

awk - Rename output file, after processing, same as input file

I have one input file ABC.txt and one output DEF.txt. After the ABC is processed and created output, I want to rename ABC.txt to ABC.orig and DEF to ABC.txt. Currently when I am doing this, it does not process the input file as it cannot read and write to the same file. How can I achieve this? ... (12 Replies)
Discussion started by: High-T
12 Replies

4. UNIX for Advanced & Expert Users

Wanted to replace string in an .xlsx file in multiple ZIP Files

Hi , I am having a ZIP file containing an .xlsx file . Now i wanted to replace "GJ" to blank in the .xlsx file . I tried using the below code but not working , Please guide : #!/bin/bash log="/home/srikant/scripts/replacescriptFHO.log" date > $log echo "" >> $log echo initiating for FHO... (1 Reply)
Discussion started by: vipinmaster
1 Replies

5. Shell Programming and Scripting

Wanted to download the yesterday's file through FTP

Hello, Wanted to download the yesturday's or current date-1 file through shell script Mode: FTP OS: AIX if today is 13th May then file would be available as below. File Pattern:201401148682_daily_2014-05-12.txt.zip Help.. (4 Replies)
Discussion started by: Riverstone
4 Replies

6. UNIX for Advanced & Expert Users

Wanted best way to validate delimited file records

actually i post about this issue before but many folkz miss-understood with my quesion, We are checking for the delimited file records validation Delimited file will have data like this: Aaaa|sdfhxfgh|sdgjhxfgjh|sdgjsdg|sgdjsg| Aaaa|sdfhxfgh|sdgjhxfgjh|sdgjsdg|sgdjsg|... (3 Replies)
Discussion started by: Seshendranath
3 Replies

7. UNIX for Dummies Questions & Answers

look for specific values in a file and rename file with value found

Hi, i have a file with some data ..look for some specific value in the file and if found that value rename the file with the value found in the file.. ex.. File.txt 1236 43715825601ANDERSSON, 1236 437158256031963040120060901200609010000000 1236 43715825604123 MCCL AVE UPPER 1236 ... (11 Replies)
Discussion started by: dssyadav
11 Replies

8. Shell Programming and Scripting

wanted to find both link file and ordinary file using single find command

find . -type fl o/p is only the ordinary file. where in it wont give the link files. (2 Replies)
Discussion started by: nikhil jain
2 Replies

9. Shell Programming and Scripting

A script that will move a file to a directory with the same name and then rename that file

Hello all. I am new to this forum (and somewhat new to UNIX / LINUX - I started using ubuntu 1 year ago).:b: I have the following problem that I have not been able to figure out how to take care of and I was wondering if anyone could help me out.:confused: I have all of my music stored in... (7 Replies)
Discussion started by: marcozd
7 Replies

10. UNIX for Dummies Questions & Answers

Help with multiple file rename - change case of part of file name

Hi there, I hope someone can help me with this problem : I have a directory (/var/www/file/imgprofil) which contains about 10000 JPG files. They have a naming convention thus : prefix-date-key-suffix.jpg they all have the prefix p-20050608- then AAAA is a 4 letter code the suffix is... (7 Replies)
Discussion started by: steve7
7 Replies
Login or Register to Ask a Question