need help with small script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers need help with small script
# 1  
Old 06-02-2011
need a script which can update month and year in the file

Hi I have the below file
Name=abc
Yr=2011
Mon=05

My script should be in such a way that whenever I run it then
Month should increment by 1 and if the month is 12 then when
I run the script then year should incremented by 1 and month
Should become 01(I.e jan)

Thanks for the help in advance

---------- Post updated at 01:06 PM ---------- Previous update was at 12:01 PM ----------

sorry if I did not mention my requirement properly
Please refer the below scenarios

Scenario 1
================
source file
-----------
Name=abc
Yr=2011
Mon=05

desired output after 1st run of the script
---------------------------------------
Name=abc
Yr=2011
Mon=06


Scenario 2
============================
source file
-----------
Name=abc
Yr=2011
Mon=12

desired output after 1st run of the script
---------------------------------------
Name=abc
Yr=2013
Mon=01

so my requirement is that everytime when I run the script then the month should be incremented by 1 and if month is 12 in the source file then after running the script year should be incremented by 1 and month should become 1.

Please help me

Last edited by kishu; 06-02-2011 at 03:56 PM..
# 2  
Old 06-02-2011
Code:
#!/usr/bin/ksh
typeset -i mYYYY=$(sed -n 's/Yr=//p' input_file)
typeset -i mMM=$(sed -n 's/Mon=//p' input_file)
if [[ ${mMM} -eq 12 ]]; then
  mMM=1
  mYYYY=${mYYYY}+1
else
  mMM=${mMM}+1
fi
sed -e "s/Yr=.*/Yr=${mYYYY}/" -e "s/Mon=.*/Mon=${mMM}/" input_file

# 3  
Old 06-02-2011
Thank you very much
its working fine but only problem is that month is coming as single digit i.e. if it is "05" after running the script it is updated as as "6" but i need it to be "06"
# 4  
Old 06-02-2011
Try this one:
Code:
#!/usr/bin/ksh
typeset -i mYYYY=$(sed -n 's/Yr=//p' input_file)
typeset -i mMM=$(sed -n 's/Mon=//p' input_file)
if [[ ${mMM} -eq 12 ]]; then
  mMM=1
  mYYYY=${mYYYY}+1
else
  mMM=${mMM}+1
fi
mMM2=$(printf "%.2d" ${mMM})
sed -e "s/Yr=.*/Yr=${mYYYY}/" -e "s/Mon=.*/Mon=${mMM2}/" input_file

# 5  
Old 06-02-2011
Thank you very much Its working Smilie
need final help,
I need to UPDATE the file with these changes i.e,source file needs to be updated with these new values after the script runs.
# 6  
Old 06-02-2011
Here is one way of updating the file:
Code:
...
sed -e "s/Yr=.*/Yr=${mYYYY}/" -e "s/Mon=.*/Mon=${mMM2}/" input_file > tmp_file
mv tmp_file > input_file

# 7  
Old 06-02-2011
Yup...Its working very well...
thank you very much for the help...
today i learned few things in UNIX,
thank you once again
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help to understand this small script

Hi Guys, I need to understand below scipt:- -bash-3.00$ cat rsync-copy.ksh #!/usr/5bin/ksh batch <<%EOF% echo "/usr/local/bin/rsync --rsync-path=/usr/local/bin/rsync -a --stats /usr/openv/ /OpenvBCK" > openv.LOG # CG /usr/local/bin/rsync ... (6 Replies)
Discussion started by: manalisharmabe
6 Replies

2. Shell Programming and Scripting

small script correction

input cz1 87942437 87952030 M_001144992 0 + 87942537 87949664 0 3 710,114,2506, 0,2725,7087, script awk '{ n11 = split($11, t11, ",") n12 = split($12, t12, ",") for (i = 0; ++i < n11;) { s12 = $2 + t12 print $4"_xon"i, "\t",$4"_xon"i,"\t", $1,... (1 Reply)
Discussion started by: quincyjones
1 Replies

3. AIX

Need help in a small script

Hello all, could somebody help..? I have following 6 files (with white spaces in their names) This is file This is file1 This is file2 This is file3 This is file4 This is file5 This is file6 I tried to run the below script, and it did not give me desired ouput.. $ for i in `ls -1` >... (7 Replies)
Discussion started by: gsabarinath
7 Replies

4. Shell Programming and Scripting

need a small script

Hello all, i have a batmail process running on my machine(java process). i just need a script we should detect whether the batchnail is running or not.If not it should restart it. Can anyone assist me on this? (1 Reply)
Discussion started by: Rayzone
1 Replies

5. Shell Programming and Scripting

Small Search script

I need to write a small shell script which does the following : I have a file : root/var/log/ocmp/ocmpclient.log This is a log file which is continuosly getting updated . I have to keep looking into this file all the time. I have to look for four keywords, "File Detected", File Sending",... (2 Replies)
Discussion started by: appu1987
2 Replies

6. Shell Programming and Scripting

small script help

#!/bin/ksh for i in *.log* do ls $i|sed 's/\.log$//g' | while read file do echo "file $file is Running" >> lls.txt echo "***************">> lls.txt done done ------------------------------------------------------------------ the output is : file AdapterCCProvisioningInterface... (9 Replies)
Discussion started by: ali560045
9 Replies

7. Shell Programming and Scripting

Very small Shell Script Help...

The following Script takes each extension and determine what category it belongs and then moves it into a directory based on the extension. (for eg. 1.sh, 5.sh, 9.sh together; 4.csh, 120.csh, 6.csh together and 7.ksh, 2.ksh, 59.ksh together) and moves them to their respective directories viz.... (2 Replies)
Discussion started by: marconi
2 Replies

8. Shell Programming and Scripting

small script

Hi, I am new to unix shell scripting. I just want a little script to check the no. of processes are equal to 8, then echo a successful message otherwise echo a unsuccessful message. Please help. Thanks. (3 Replies)
Discussion started by: everurs789
3 Replies

9. Shell Programming and Scripting

small script help

here is a small script: if ; then echo please enter an argument fi if [ "$1" = "tom"; then a=$1 echo $a fi here is my question. if the script name is j.sh and I run it : j.sh from shell prompt: without a parameter: it prints please enter an argument but if I go with . j.sh (current... (1 Reply)
Discussion started by: rkl1
1 Replies

10. Shell Programming and Scripting

Need help in a small script

Hi all, I have a file of the following format - EXPRPT:SCN:1.1706E+10:SEQ_START:121652:SEQ_END:121664:0 ( This file name is variable and changes daily) Now in the same directory I have another set of files of the format - EXPRPT.log.0001.0000121669 Now what I am trying to do is to ... (2 Replies)
Discussion started by: super_duper_guy
2 Replies
Login or Register to Ask a Question