Changing file names with AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Changing file names with AWK
# 1  
Old 12-07-2009
Changing file names with AWK

Dear All,

I have some thousands of files in a folder and i need to change those file names without opening the file (no need to change anything in the file content, need to change the file name only). The filenames are as follows:

Myfile_name.1_parameter
Myfile_name.2_parameter
Myfile_name.3_parameter
.
.
.
.
Myfile_name.100_parameter
.
.
.
.
.
.
Myfile_name.1000_parameter
.
.
.
.
..
Myfile_name.2000_parameter...and so on.

For other calculation, i need to take these files one by one and do the calculations. But these files are arranged in the following manner (OS-Linux):
Myfile_name.10_parameter
Myfile_name.11_parameter
Myfile_name.12_parameter
.
.
.
Myfile_name.100_parameter
Myfile_name.101_parameter
Myfile_name.102_parameter
.
.
.
Myfile_name.1000_parameter
Myfile_name.1001_parameter
Myfile_name.1002_parameter
.
.
.
.
.
Myfile_name.1_parameter
Myfile_name.20_parameter
Myfile_name.21_parameter ...so on and so forth...

I need to change these file names as follows:

Myfile_name.0001_parameter
Myfile_name.0002_parameter
Myfile_name.0003_parameter
...
...
...
Myfile_name.0100_parameter
...
...
Myfile_name.0200_parameter
..
..
..
Myfile_name.1000_parameter
...
...
...
...
Myfile_name.2000_parameter...an so on.

Simply i need to covert the filename with four digit numbers in it instead of one or two or three digit number in the filename.

Can any one help me to do the same using awk?

Expecting your reply and thanks in advance.

Warm regards
Fredrick.
# 2  
Old 12-07-2009
Quote:
Originally Posted by Fredrick
Can any one help me to do the same using awk?

Expecting your reply and thanks in advance.
Please post your code you have tried first.

Thanks.
# 3  
Old 12-07-2009
the code i have tried is here

Code:
for i in *_parameter
do
  j=$(printf "%03d.%s" ${i%.*} ${i#*.})
  [[ $i != $j ]] && mv $i $j
done

Thanks
Fredrick.
# 4  
Old 12-07-2009
Wrench

Try this
Code:
for FILE in * # or whatever you want to select the files
do
    EXT=${FILE##*.}
    N=${EXT%_*}
    END=${EXT#$N}
    NEWNAME="${FILE%$EXT}$(printf '%04d\n' $N)$END"
    echo "FILE=$FILE  - NEWNAME=$NEWNAME"
#    mv "$FILE" "$NEWNAME" # Uncomment this line to really rename files
done

# 5  
Old 12-07-2009
Code:
for FILE in *_parameter
do
  NEW=`echo $FILE |awk -F[._] '{printf "%s_%s.%04d_%s",$1,$2,$3,$4}' `
#    mv "$FILE" "$NEW"  # Uncomment this line to really rename files
done

# 6  
Old 12-08-2009
Thank you very much for your reply. Its giving me syntax error - unterminated regexp. here is the code

Code:
#!/usr/bin/awk

for FILE in *_CA_torsion
do
  NEW=`echo $FILE |awk -F[._] '{printf "%s_%s.%04d_%s",$1,$2,$3,$4}'`;
  mv "$FILE" "$NEW"
done

Expecting your reply and thanks in advance.

Warm Regards
Fredrick

---------- Post updated at 02:14 PM ---------- Previous update was at 02:11 PM ----------

Sorry semicolon ";" in the above code is typo error.

Thanks & Regards
Fredrick.
# 7  
Old 12-09-2009
Try this:

Code:
ls -1 Myfile_name*parameter | awk -F"[._]" '{ printf "mv Myfile_name.%d_parameter Myfile_name.%04d_parameter\n" ,$3,$3 }' | sh

If there are many files and wild char cannot able to expand , use find

ie, instead of ls -1 Myfile_name*parameter , use find . -name "Myfile_name*parameter"

If you are concerned about performance, try using direct syscall to execute a temp script contain the commands

Code:
ls -1 Myfile_name*parameter | awk -F"[._]" '{ printf "mv Myfile_name.%d_parameter Myfile_name.%04d_parameter\n" ,$3,$3 >>"file_out"} END { system("sh file_out") } '


Last edited by dennis.jacob; 12-09-2009 at 08:50 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Changing file names

sac_pzs_iv_epoz_hhe__2013.074.14.40.46.0000_2599.365.23.59.59.99999 sac_pzs_iv_epoz_hhn__2013.074.14.40.46.0000_2599.365.23.59.59.99999 sac_pzs_iv_epoz_hhz__2013.074.14.40.46.0000_2599.365.23.59.59.99999 sac_pzs_iv_haga_hhe__2006.111.00.00.00.0000_2599.365.23.59.59.99999... (3 Replies)
Discussion started by: kristinu
3 Replies

2. Debian

Changing file extension names

Hi I have a list of files :root@L28mustang:/var/log/exim4/2017/Jul2017_Blast_BC07# ls -lrt | grep mainlog -rw-r----- 1 Debian-exim adm 3636932 Jul 8 06:25 mainlog.3.gz -rw-r----- 1 Debian-exim adm 919512 Jul 9 06:27 mainlog.2.gz -rw-r----- 1 Debian-exim adm 7655054 Jul 10 06:25 mainlog.1... (1 Reply)
Discussion started by: anaigini45
1 Replies

3. UNIX for Dummies Questions & Answers

Bulk changing of file names using Terminal in OS X

I want to change the name of several files within a folders (directory) and subdirectories in OS X. If I only wanted to change file names within the directory I guess I would use: rm photo*.jpg picture*.jpg I have lots of subdirectories, is there a way of getting the file changes for all... (5 Replies)
Discussion started by: ademanuele
5 Replies

4. Shell Programming and Scripting

Changing file names

I have a series of files as follows file-1.pdf file-2.pdf file-3.pdf file-4.pdf file-5.pdf file-6.pdf file-7.pdf I want to have the file names with odd numbers starting from an initial number, for example 2000. The result would be the following: file-2001.pdf file-2003.pdf... (9 Replies)
Discussion started by: kristinu
9 Replies

5. Shell Programming and Scripting

Changing file names

I have file names as shown and want to change the name to have only the first four numbers. /home/chrisd/Desktop/nips/nips_2013/5212-learning-feature-selection-dependencies-in-multi-task-learning.pdf /home/chrisd/Desktop/nips/nips_2013/5213-parametric-task-learning.pdf... (3 Replies)
Discussion started by: kristinu
3 Replies

6. Shell Programming and Scripting

changing multiple directory names

Hi guys, I have lots of files that look like: ABC.packed.dir DEF.packed.dir GHI.packed.dir etc... I would like them to have more of the usual naming convention ABC DEF GHI etc... so I was thinking that I could: (2 Replies)
Discussion started by: atjurhs
2 Replies

7. Shell Programming and Scripting

[SOLVED] Changing file names

I have written a csh script that changes the name of file from src to dst. I am getting the error below: TESTAmvfiles DONE TESTAmvfiles set: Variable name must begin with a letter. The csh script is: #!/bin/csh #... (0 Replies)
Discussion started by: kristinu
0 Replies

8. Shell Programming and Scripting

Changing file names

I have lot of files whose names are something like the following. I want to change the name of all the files from 'npt02' to 'n02'. npt02-z30-sr65-rgdt0p50-dc0p01-16x12drw.tpf npt02-z30-sr65-rgdt0p50-dc0p01-8x6drw.back npt02-z30-sr65-rgdt0p50-dc0p01-8x6drw-bst-mis.xy... (5 Replies)
Discussion started by: kristinu
5 Replies

9. Shell Programming and Scripting

Changing names

I have file names m04-npt06-z30-syn.ps m04-npt06-dp018-8x6smp.vmod m04-npt06-sr40-syn-dp01-16x12drw.params m04-npt06-sr40-syn-dp008-16x12drw.params m04-npt06-sr40-syn-dp008-16x12drw.vmod m04-npt06-sr40-syn-dp008-16x12drw.bck m04-npt06-sr40-syn-dp008-16x12drw.exp... (6 Replies)
Discussion started by: kristinu
6 Replies

10. Shell Programming and Scripting

changing file names to lowercase

hey guys having some trouble figuring this out. my program is supposed to take a name of a directory as a command line argument and change the filenames inside that directory to lowercase. what i dont get is how you access that directory and go thru all the files and change the filenames... (1 Reply)
Discussion started by: 30177005
1 Replies
Login or Register to Ask a Question