Rename file of special type


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Rename file of special type
# 1  
Old 03-06-2013
Rename file of special type

HI ! all
till date I usually rename file like this

Code:
n=201108290000
for file in *.nc; do
    file_name=M.m.1.1.1.$n.ready
    n=$(( $n+1 ))
    mv $file $file_name
done

but in this case I have to rename file depending on basename of file, when I list files results like this, if there is leap year then also it should rename properly

Code:
S19972441997273.L3m_MO_CHL_chlor_a_9km.G3.nc
S19972741997304.L3m_MO_CHL_chlor_a_9km.G3.nc
S19973051997334.L3m_MO_CHL_chlor_a_9km.G3.nc
S19973351997365.L3m_MO_CHL_chlor_a_9km.G3.nc
S19980011998031.L3m_MO_CHL_chlor_a_9km.G3.nc
S19980321998059.L3m_MO_CHL_chlor_a_9km.G3.nc
S19980601998090.L3m_MO_CHL_chlor_a_9km.G3.nc
S19980911998120.L3m_MO_CHL_chlor_a_9km.G3.nc
S19981211998151.L3m_MO_CHL_chlor_a_9km.G3.nc
S19981521998181.L3m_MO_CHL_chlor_a_9km.G3.nc
S19981821998212.L3m_MO_CHL_chlor_a_9km.G3.nc
S19982131998243.L3m_MO_CHL_chlor_a_9km.G3.nc
S19982441998273.L3m_MO_CHL_chlor_a_9km.G3.nc
S19982741998304.L3m_MO_CHL_chlor_a_9km.G3.nc
S19983051998334.L3m_MO_CHL_chlor_a_9km.G3.nc
S19983351998365.L3m_MO_CHL_chlor_a_9km.G3.nc

expecting output like this

Code:
September_1997.nc
October_1997.nc
November_1997.nc
December_1997.nc
January_1998.nc
February_1998.nc
March_1998.nc
April_1998.nc
May_1998.nc
June_1998.nc
July_1998.nc
August_1998.nc
September_1998.nc
October_1998.nc
November_1998.nc
Decembe_1998.nc

and after renaming I want to sort files in ascending order.
# 2  
Old 03-06-2013
So you want to rename them from filenames which sort, into filenames which don't sort... Then sort them? I suspect there's a reason they're named the way they are.

Would a compromise be possible, like YYYYMMDD-September-1998? That would still sort...
# 3  
Old 03-06-2013
I really, really like riddles or puzzles or however you may call those correctly. But this one - brilliant. 01. Sep 1997 is day of year 244, 30. Sep 1997 is 273, so the first file is for 01.Sep - 30.Sep 1997, second for 01.Oct - 31.Oct 1997, and so forth. Having found an algorithm to calculate Month-Year from the first seven digits, you're done - what problems do you expect from leap years?
And, yes, WHAT ascending order do you mean?
# 4  
Old 03-07-2013
@ RudyC
here after renaming all files I want to sort like this
Code:
september_1997.nc
october_1997.nc
.....
.....
December_1998.nc

if leap year then 32-60 days,,,if not 32-59, for Feb..then march starts from 61 or 60 depending on whether leap or not,may be some function required so I just worried...
# 5  
Old 03-07-2013
This is my try on it.. Smilie

I've created one file from cal

Code:
$ cat file_month
January 2011 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
February 2011 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
March 2011 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
April 2011 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
May 2011 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
June 2011 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
July 2011 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
August 2011 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
September 2011 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
October 2011 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
November 2011 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
December 2011 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

Code:
$ cat file
S19972441997273.L3m_MO_CHL_chlor_a_9km.G3.nc
S19972741997304.L3m_MO_CHL_chlor_a_9km.G3.nc
S19973051997334.L3m_MO_CHL_chlor_a_9km.G3.nc
S19973351997365.L3m_MO_CHL_chlor_a_9km.G3.nc
S19980011998031.L3m_MO_CHL_chlor_a_9km.G3.nc
S19980321998059.L3m_MO_CHL_chlor_a_9km.G3.nc
S19980601998090.L3m_MO_CHL_chlor_a_9km.G3.nc
S19980911998120.L3m_MO_CHL_chlor_a_9km.G3.nc
S19981211998151.L3m_MO_CHL_chlor_a_9km.G3.nc
S19981521998181.L3m_MO_CHL_chlor_a_9km.G3.nc
S19981821998212.L3m_MO_CHL_chlor_a_9km.G3.nc
S19982131998243.L3m_MO_CHL_chlor_a_9km.G3.nc
S19982441998273.L3m_MO_CHL_chlor_a_9km.G3.nc
S19982741998304.L3m_MO_CHL_chlor_a_9km.G3.nc
S19983051998334.L3m_MO_CHL_chlor_a_9km.G3.nc
S20003351998365.L3m_MO_CHL_chlor_a_9km.G3.nc

Code:
$ awk 'NR==FNR{for(i=2;i<=NF;i++){B[++a]=$1}next}{
if(substr($1,2,4)%4){a=substr($1,13,3)+0}else{a=substr($1,13,3)>31?substr($1,13,3)+1:substr($1,13,3)+0}
{print B[a]"_"substr($1,2,4)".nc"}}' file_month file

September_1997.nc
October_1997.nc
November_1997.nc
December_1997.nc
January_1998.nc
February_1998.nc
March_1998.nc
April_1998.nc
May_1998.nc
June_1998.nc
July_1998.nc
August_1998.nc
September_1998.nc
October_1998.nc
November_1998.nc
December_2000.nc

Now we have to implement this logic to just rename those files...

Hope this helps you.. Smilie

pamu

Last edited by pamu; 03-07-2013 at 03:06 AM..
# 6  
Old 03-07-2013
Code:
$ awk -F"." -v ext=".nc" '
BEGIN {
arr[1]=31;arr[3]=31;arr[4]=30;arr[5]=31;arr[6]=30
arr[7]=31;arr[8]=31;arr[9]=30;arr[10]=31;arr[11]=30;arr[12]=31
mth[1]="January";mth[2]="February";mth[3]="March"
mth[4]="April";mth[5]="May";mth[6]="June"
mth[7]="July";mth[8]="August";mth[9]="September"
mth[10]="October";mth[11]="November";mth[12]="December"
}
{
yr=substr($1,9,4)
day=substr($1,13,3)
if ( yr % 4 == 1 ) { arr[2]=28 } else { arr[2]=29 }
for(i=1;day>0 && i<=12;++i){ day=day-arr[i] }
print mth[i-1] "_" yr ext;
} ' file
September_1997.nc
October_1997.nc
November_1997.nc
December_1997.nc
January_1998.nc
February_1998.nc
March_1998.nc
April_1998.nc
May_1998.nc
June_1998.nc
July_1998.nc
August_1998.nc
September_1998.nc
October_1998.nc
November_1998.nc
December_1998.nc

# 7  
Old 03-07-2013
You couldn't miss aiming at the center of a broad target like a +- 30 day month, even if one month's length is off by a day in leap years. The center is the 15th of each month, or the average of month start and month end, taken to calculate the index into the months' names array:
Code:
awk -F. 'BEGIN {split ("Jan.Feb.Mar.Apr.May.Jun.Jul.Aug.Sep.Oct.Nov.Dec", MNames)}
         {Idx  = int((substr ($1, 6, 3) + substr ($1, 13, 3)) / 60) + 1
          Year = substr ($1, 2, 4)
          print $0, " ---> ", MNames[Idx] "_" Year ".nc"}
        ' file

You could even strip that down to the bare minimum:
Code:
awk -F. 'BEGIN {split ("Jan.Feb.Mar.Apr.May.Jun.Jul.Aug.Sep.Oct.Nov.Dec", MNames)}
         {print MNames[int((substr ($1, 6, 3) + substr ($1, 13, 3)) / 60) + 1] "_"  substr ($1, 2, 4) ".nc"}
        ' file

I've taken the input stream to be sorted; if that's not the case, run it through a simple sort step first.
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Rename File Name with Special Characters

I am trying to rename files with spaces and other characters and not able to be successful. FileNames: UPLOAD REFERENCE.xls UPLOAD MASS REFERENCE.XLS find /UPLOAD REFERENCE/ -depth -type f -name "* *" -exec rename " " "_" "{}" ";" The above one is successful to replace spaces... (1 Reply)
Discussion started by: eskay
1 Replies

2. Shell Programming and Scripting

Shell script to read file and check file type

Hi, I have a file with few values in it. I need script help to read file line by line and check: 1/if it's a file (with extension eg .java .css .jar etc ) or 2/if it's a file without extension and treat it as a directory and then check if the directory exists in working copy else create one... (6 Replies)
Discussion started by: iaav
6 Replies

3. Shell Programming and Scripting

[solved] File type error (not a regular file)

Hi friend, i have written script as below to check the file existance. but i got error path="/k/p1100/users/jewel/Output" FILENAME=`ls -lrt $path/*HT|tail -1|cut -d "/" -f 8` if ; then echo "$FILENAME is available " chmod 755 $path/$FILENAME /usr/bin/scp... (0 Replies)
Discussion started by: Jewel
0 Replies

4. Shell Programming and Scripting

Rename files and directories with special characters

Hello guys, I was looking for a shell script that removes all the special characters from the files and the subdirectories recursively. I could not locate it any more. Dose any body have a similar script that dose that? Thanks for the help. AV (0 Replies)
Discussion started by: avatar_007
0 Replies

5. Shell Programming and Scripting

How to know the file type

Hi I am doing some operation in which files will be dumped in Specific location. I want to know the File type.i.e i am expecting CSV files. So i want to write a script so that i can check the file which are dumped are in CSV Files.xxxx.csv format. (2 Replies)
Discussion started by: Aditya.Gurgaon
2 Replies

6. UNIX for Dummies Questions & Answers

rename more files special case

hello all i have a big problems for me if i have more files as example test.ghrt.part01.rar test.ghrt.part02.rar test.ghrt.part03.rar test.ghrt.part04.rar test.ghrt.part05.rar test.ghrt.part06.rar test.ghrt.part07.rar test.ghrt.part08.rar test.ghrt.part09.rar test.ghrt.part10.rar... (13 Replies)
Discussion started by: ateya
13 Replies

7. Programming

array type has incomplete element type

Dear colleagues, One of my friend have a problem with c code. While compiling a c program it displays a message like "array type has incomplete element type". Any body can provide a solution for it. Jaganadh.G (1 Reply)
Discussion started by: jaganadh
1 Replies

8. Shell Programming and Scripting

String type to date type

Can one string type variable changed into the date type variable. (1 Reply)
Discussion started by: rinku
1 Replies

9. Programming

file type

Hi Everyone! I am working on a c program which displays all the directories and files under each directory. I want to know what kind of file each is. Like, is the file an ascii text file or english text or a c file. if it is an executable, is it an binary file or a shell script. I was told to... (12 Replies)
Discussion started by: vijlak
12 Replies
Login or Register to Ask a Question