Renaming multipe files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Renaming multipe files
# 1  
Old 01-04-2012
Renaming multipe files

Hi, I have these list of files from with the following filename format (the last 3 characters are in julian days). I want the year info retained added with sequence number 01-12 to represent the month. any suggestion on how to automate the renaming process. many thanks.
Code:
original filenames  desired output
1999001.res         199901.res
1999032.res         199902.res
1999060.res         199903.res
1999091.res         199904.res
1999121.res         199905.res
1999152.res         199906.res
1999182.res         199907.res
1999213.res         199908.res
1999244.res         199909.res
1999274.res         199910.res
1999305.res         199911.res
1999335.res         199912.res
2000001.res         200001.res
2000032.res         200002.res
2000061.res         200003.res
2000092.res         200004.res
.. to
2007335.res         200712.res

# 2  
Old 01-04-2012
Run below command without red part ( |sh" ) frist , if looks fine, then run the whole command to rename files.
Code:
ls *.res |awk -F \. 'function JD2MON(YY,JD){ 
               split("31 28 31 30 31 30 31 31 30 31 30 31",m," ")
               mm = 1; JD+=0
	       if (YY%4==0 && (YY%100!=0 || YY%400==0)) m[2] = 29 
               while (JD > m[mm]) {
                     JD -= m[mm++]
               }
               return sprintf ("%4d%02d",YY,mm)
           }
           { year=substr($1,1,4)
             JulianDay=substr($1,5,3)
             MON=JD2MON(year,JulianDay)
             printf "mv %s %s\n", $0 , MON FS $2
           }'  |sh

mv 1999001.res 199901.res
mv 1999032.res 199902.res
mv 1999060.res 199903.res
mv 1999091.res 199904.res
mv 1999121.res 199905.res
mv 1999152.res 199906.res
mv 1999182.res 199907.res
mv 1999213.res 199908.res
mv 1999244.res 199909.res
mv 1999274.res 199910.res
mv 1999305.res 199911.res
mv 1999335.res 199912.res
mv 2000001.res 200001.res
mv 2000032.res 200002.res
mv 2000061.res 200003.res
mv 2000092.res 200004.res
mv 2007335.res 200712.res

This User Gave Thanks to rdcwayx For This Post:
# 3  
Old 01-04-2012
bash code:
  1. #! /bin/bash
  2.  
  3. days_reg=( 1 32 60 91 121 152 182 213 244 274 305 335 )
  4. days_leap=( 1 32 61 92 122 153 183 214 245 275 306 336 )
  5. months=( 01 02 03 04 05 06 07 08 09 10 11 12 )
  6.  
  7. for file in `ls *res | sort -n`
  8. do
  9.     st=`echo $file | cut -c5-7`
  10.     for mt in `seq 0 11`
  11.     do
  12.         if [ $st -eq ${days_reg[$mt]} -o $st -eq ${days_leap[$mt]} ]
  13.         then
  14.             mv $file `echo $file | cut -c1-4`${months[$mt]}.res
  15.             continue
  16.         fi
  17.     done
  18. done
This User Gave Thanks to balajesuri For This Post:
# 4  
Old 01-04-2012
thanks guys, tried both options. worked seamlessly,Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Move multipe files to corresponding directories

Hi, In a parent directory there are several files in the form IDENTIFIER1x IDENTIFIER1.yyy IDENTIFIER1_Z, etc IDENTIFIER2x IDENTIFIER2.yyy IDENTIFIER2_Z, etc IDENTIFIER3x IDENTIFIER3.yyy, IDENTIFIER3_Z, etcIn the same parent directory there are corresponding directories named... (7 Replies)
Discussion started by: spirospap
7 Replies

2. Shell Programming and Scripting

Renaming multiple files in sftp server in a get files script

Hi, In sftp script to get files, I have to rename all the files which I am picking. Rename command does not work here. Is there any way to do this? I am using #!/bin/ksh For eg: sftp user@host <<EOF cd /path get *.txt rename *.txt *.txt.done ... (7 Replies)
Discussion started by: jhilmil
7 Replies

3. Shell Programming and Scripting

multipe pattern

Extract of configuration file. members server1.domain1.com,server2.domain2.com,server3.domain3.com I am writing a script to remove the server members from a conf file. When I run the script the corresponding server should be removed from the file. /root/server-remove.sh... (2 Replies)
Discussion started by: anilcliff
2 Replies

4. Shell Programming and Scripting

execute multipe commands

I would like to execute multipe commands in a shell script. Please provide the commands for the below scenario: Execute command1 if command1 is succesfull, then execute command2, command3,command4 in parallel if command2, command3,command4 are success then run command 5 (3 Replies)
Discussion started by: p_gautham12
3 Replies

5. Shell Programming and Scripting

Parsing multipe data entries

Hi I am using command awk '{printf $1 " "}' PMT_GGSN_WPP_APN-20110517* > outfile The * refers to 24 files (1 generated per hour per day) with each one having the output of A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 I need... (8 Replies)
Discussion started by: rob171171
8 Replies

6. Shell Programming and Scripting

Renaming files

Can someone please help. Much appreciated!! I have 4 directories, for ex... /RUN1/ReportTable.nxt /RUN2/ReportTable.nxt /RUN3/ReportTable.nxt /RUN4/ReportTable.nxt I would like to write a for loop, to add the directory name to each ReportTable.nxt I would like for it to be: ... (5 Replies)
Discussion started by: dirttysoufff
5 Replies

7. Shell Programming and Scripting

renaming files or adding a name in the beginning of all files in a folder

Hi All I have a folder that contains hundreds of file with a names 3.msa 4.msa 21.msa 6.msa 345.msa 456.msa 98.msa ... ... ... I need rename each of this file by adding "core_" in the begiining of each file such as core_3.msa core_4.msa core_21.msa (4 Replies)
Discussion started by: Lucky Ali
4 Replies

8. UNIX for Dummies Questions & Answers

Reading only first record from the multipe directories

Hi All, I have a requirement, I had a parent directory Land under that we have sub directories Yesterday, Today and Tommorrow And we have a file test.txt under the above directories Yesterday, Today and Tommorrow The data in the file test.txt under Yesterday folder is ... (5 Replies)
Discussion started by: somu_june
5 Replies

9. Shell Programming and Scripting

Multipe conditions in if statement

I have a script that runs every 15 minutes in cron that builds a web page. It runs at 15, 28, 45 and 58 minutes past the hour. (pretty much evry 15 mins). Every 2 hours from 6:28 to 18:28 it sends out emails if it finds errors. I do not want it sending email every single time it runs, every 15... (5 Replies)
Discussion started by: spacemancw
5 Replies

10. Shell Programming and Scripting

multipe instance scenario in perl

letz say that my file has 7 records with only one field. So my file has: 11111111 000000000000000 1111 aaaabbbccc 1111111222000000 aaaaaaaa zz All i need is: 1. when the field has a repetition of the same instance(a-z or 0-9), i would consideer it to be invalid.... (4 Replies)
Discussion started by: helengoldman
4 Replies
Login or Register to Ask a Question