Naming number to months


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Naming number to months
# 1  
Old 01-06-2014
Naming number to months

Hi,

I have many files of monthly means from model simulation such as 1,2,3,4.....12, corresponding to jan, feb, mar,....., dec.
e.g. avg_1.nc, avg_2.nc, ............., avg_12.nc.
i want to write a shell script such that it should conveet number into months and year such as..
avg_jan_2001,avg_feb_2001,......,avg_dec_2001.

how can i do it using shell script ?

thanks
# 2  
Old 01-06-2014
Code:
perl -npi.bak -e 'BEGIN{@mon=qw(null jan feb mar apr may jun jul aug sep oct nov dec);} s/avg_(\d).nc/avg_$mon[$1].nc/g;' monthly_means.dat

Remove the .bak to stop creating backups when you're satisfied it works as expected.
# 3  
Old 01-06-2014
Are you trying to rename these files then?

Well, as a starter I could suggest the case statement. Assuming that you are sh, ksh or bash it would be something like this:-
Code:
case month_num in
   1) month_name=jan ;;
   2) month_name=feb ;;
   3) month_name=mar ;;
   4) month_name=apr ;;
   5) month_name=may ;;
   6) month_name=jun ;;
   7) month_name=jul ;;
   8) month_name=aug ;;
   9) month_name=sep ;;
   10) month_name=oct ;;
   11) month_name=nov ;;
   12) month_name=dec ;;
   *) echo "Invalid month value" ;;
esac

You will need to chop out the number from the incoming file name as month_num and the afterwards, you can use $month_name as you wish.



I hope that this helps. Let us know how you get on and if you need any more guidance.



Robin
Liverpool/Blackburn
UK

Last edited by rbatte1; 01-06-2014 at 06:04 AM.. Reason: Spling mustike
# 4  
Old 01-06-2014
Hi,

For example, I have 24 files corresponding to JAN to DEC 2001 and JAN to DEC 2002.

the files are avg_1.nc avg_2, ....... avg_24.nc.

I want to rename it as

avg_jan_2001, avg_feb_2001,...........,avg_dec_2002.nc

I am trying it for first year as...
Code:
#!/bin/sh

year_st=2001
year_en=2001
year=$year_st
while [ $year -le $year_en ]
do
 for var in jan feb mar apr may jun jul aug sep oct nov dec
   do
      echo $var_$year
     
   done
     year = $(( year_st+1 ))
 done

But it gives me only 2001 without end.

can anybody modify this script for two years ?

thanks

Last edited by Franklin52; 01-06-2014 at 07:33 AM.. Reason: Please use code tags
# 5  
Old 01-06-2014
A few issues here:-
  • Your value for year_en is 2001 so the range is 2001 to 2001
  • You increase the value year by adding one to the value year_st, which means you could have an infinite loop if year_st does not equal year_en
  • You should really use { and } to wrap your variables when you are using them, such that your echo becomes echo ${var}_${year}Does this give you anything further to work on?



    I hope that this helps you.


    Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with naming the file

Hi, I have a folder that contains files abc.txt def.txt ....and so on Inside abc.txt, I have @<TRIPOS>MOLECULE 4|Chelerythrine|abcb11_earlyIdentification_Stronginhib_washed_ligprep|sdf|1|dock Inside def.txt, I have @<TRIPOS>MOLECULE... (6 Replies)
Discussion started by: rossi
6 Replies

2. What is on Your Mind?

Humorous naming

Dear all, We've been asked to submit names for our documentation system. It used to be the very dry ISDL (Information Services Documentation Library) The replacement is built on a Wiki-beastie but that doesn't help much with a name. I wondered about an acronym based on CRAFT, so I can... (1 Reply)
Discussion started by: rbatte1
1 Replies

3. Shell Programming and Scripting

Count number of errors within logs for last 6 months

I have directory /test/logs which has multiple logs: audit.log audit.log.1 audit.log.2 audit.log.3 audit.log.4 audit.log.5 audit.log is current log file and audit.log.X are archive log files. I need to search within these log files and count word "error-5" logged within last 6 months... (4 Replies)
Discussion started by: djanu
4 Replies

4. Shell Programming and Scripting

naming columns

i have a file staff.txt with contents tom|25|New York sims|40|London neyo|18|Moscow i want to label the column at the top, my output should be Names|age|city of birth tom|25|New York sims|40|London neyo|18|Moscow (4 Replies)
Discussion started by: blackzinga80
4 Replies

5. Shell Programming and Scripting

#file naming

hi all, Please advise at what circumstance those file will become -rwxr-xr-x 1 psa psa 1969088 Aug 18 2006 #libaa.sl -rwx------ 1 psa psa 2166784 Jul 25 2006 #libcrypto.sl.0.9.7 -rwx------ 1 psa psa 904040 Jul 25 2006 #libxxx.sl -rwx------ 1 psa ... (2 Replies)
Discussion started by: rauphelhunter
2 Replies

6. Shell Programming and Scripting

some months ego

Hi, how can I format date function to find n (n=1 to 11) months ego ? For exemple now we are in july and I want to have a variable presenting april. How should it be : month=`date +%b`????? Many thanks before. (8 Replies)
Discussion started by: big123456
8 Replies

7. UNIX for Advanced & Expert Users

Comparing two months

Hi all, Using shell script and awk statements, I manage to get the month and year of the Patch Bundle installed on the system. Now I have to compare month and year of Patch Bundle installed to the Patch Bundle which is desired on the workstation. I am able to do easily comparison of year but not... (1 Reply)
Discussion started by: pankschawla
1 Replies

8. UNIX for Advanced & Expert Users

Controller Naming

Hello all, How does the Solaris identifies the controller subscript ? ( like c0txdxs0 or c1txdxsx ?? ) I have a unix box ( Ultra 30) running with 2.5.1. When I connected an external hard disk to the on-board scsi port, it got identified as c0t1dxsx... (... (1 Reply)
Discussion started by: shibz
1 Replies

9. IP Networking

DNS Help - Been trying to do this for months

Ok someone has to be able to help me here. I have the latest webmin, and i have bind i have a domain name called thedigitaldream.co.uk i have one server, just one as im poor so the one server is gunna be the dns, email, web and ftp server. Its only for a small site anyway so it will... (16 Replies)
Discussion started by: matt2kjones
16 Replies
Login or Register to Ask a Question