Sponsored Content
Top Forums Shell Programming and Scripting Substract 1 Month from MonthID Post 302900450 by Don Cragun on Wednesday 7th of May 2014 01:27:47 AM
Old 05-07-2014
Quote:
Originally Posted by pinnacle
Hi,
I am writing a small unix command to substract one month from the MonthID.
If MonthID = 201301 the below script returns me 201212
Code:
 
if [[ `echo $monthid | cut -c5-6` == 01 ]] then
a=`echo $monthid | cut -c1-4`
b=`expr $a - 1`
c=12
echo "$b$c"
else 
a=`echo $monthid | cut -c5-6`
b=`expr $a - 1`
c=`echo $monthid | cut -c1-4`
echo "$c$b"
fi

Problem exists when MonthID=201307 and i run the above script and it returns 20136 instead of 201306
Help is appreciated to get the MonthID in correct format.
Moderator's Comments:
Mod Comment Please use ICODE tags, not QUOTE tags, when displaying in-line code, input, and output samples.
Hi pinnacle,
I find this very confusing.

You haven't said what shell or operating system you're using, but from the [[ expression ]], I assume that you're not using a Bourne shell and that you are probably using bash or ksh.

But, your code sample doesn't define MonthID (or monthid). And the way you define MonthID in the description of what you're trying to do (with spaces around the equals sign in MonthID = 201301 won't work with any shell that I know of that supports [[ ... ]].

If my assumptions are correct:
  1. You are using a Korn shell, a bash shell, or another shell that recognizes POSIX arithmetic expansions, and
  2. you want to work with a variable named MonthID, not monthid,
then the following should do what you want MUCH more efficiently:
Code:
MonthID=${1:-201301}
printf 'Starting MonthID: %d\n' "$MonthID"
MonthID=$((MonthID - 1))
if [ $((MonthID % 100)) -eq 0 ]
then	MonthID=$((MonthID - 88))
fi
printf "Ending MonthID: %d\n" "$MonthID"

In fact, with a bash or ksh shell, it can be simplified further to:
Code:
MonthID=${1:-201301}
printf 'Starting MonthID: %d\n' "$MonthID"
if [ $((--MonthID % 100)) -eq 0 ]
then	((MonthID -= 88))
fi
printf "Ending MonthID: %d\n" "$MonthID"

Quote:
Originally Posted by Aia
substitute echo for printf
Code:
printf "%d%02d\n" $c, $b

Hi Aia,
There is no need for the comma in $c,, and with some shells having a comma there will generate a syntax error.
These 2 Users Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Substract date (month) Problem

#!/bin/ksh month=`date | cut -c5-8` year=`date | cut -c24-28` echo "$month" echo "$year" --- This gives me output as Feb and 2009 but now I want to substract the 1 month from the current script and want output as Jan 2009. Please note I have searched a lot on forum and found... (5 Replies)
Discussion started by: niceboykunal123
5 Replies

2. UNIX for Dummies Questions & Answers

Substract a certain number to the names of several files

We have a list of files (raw and jpeg types) with 2 different extensions (rw2 and jpg). When there is both the raw and jpeg files, their file numbers must be the same (215 and 215, 218 and 218). Sometimes there is only the jpeg file (216,217). bla_215.rw2 bla_215.jpg bla_216.jpg bla_217.jpg... (9 Replies)
Discussion started by: Epictete
9 Replies

3. Shell Programming and Scripting

substract column based on some criteria

Please guide if you know how to solve this. I have a tab delimited INPUT FILE where each record is separated by ----- ----- ABC 4935402 4936680 Pattern=Cheers07080.1 ABC 4932216 4932368 Pattern=Cheers07080.1 ABC 4931932 4932122 ... (8 Replies)
Discussion started by: sam_2921
8 Replies

4. UNIX for Dummies Questions & Answers

print previous month (current month minus 1) with Solaris date and ksh

Hi folks month=`date +%m`gives current month Howto print previous month (current month minus 1) with Solaris date and ksh (7 Replies)
Discussion started by: slashdotweenie
7 Replies

5. Solaris

Substract time between two columns

I've start and end time in two columns. How can I substract time from column 2 and column 1 and receive output in another file 'd' ? $ cat c 12:55:04 2:03:56 2:03:56 3:20:17 14:00:00 13:05:00 (1 Reply)
Discussion started by: alps0206
1 Replies

6. Shell Programming and Scripting

substract variable from each line in a file

Hi everyone, I have a file containing one column and I want to subtract the first value (value of first line) from each line's value. 79304 99299 119294 139289 159284 179279 199274 219269 239264 259259 279254 299249 319244 339239 359234I tried working on an awk solution with... (1 Reply)
Discussion started by: ink_LE
1 Replies

7. Shell Programming and Scripting

Script to counting a specific word in a logfile on each day of this month, last month etc

Hello All, I am trying to come up with a shell script to count a specific word in a logfile on each day of this month, last month and the month before. I need to produce this report and email it to customer. Any ideas would be appreciated! (5 Replies)
Discussion started by: pnara2
5 Replies

8. Shell Programming and Scripting

Substract and print

Dear all, I need your help. I have file input like this: input.txt: R1031 50111G1 R1031 50121G1 R1031 50131G1 R1031 50141G1 R1031 50151G1 . . . . Desired output: 10315011 = G, 10315012 =... (2 Replies)
Discussion started by: attila
2 Replies

9. Shell Programming and Scripting

How to add decimal month to some month in sql, php, perl, bash, sh?

Hello, i`m looking for some way to add to some date an partial number of months, for example to 2015y 02m 27d + 2,54m i need to write this script in php or bash or sh or mysql or perl in normal time o unix time i`m asking or there are any simple way to add partial number of month to some... (14 Replies)
Discussion started by: bacarrdy
14 Replies

10. Shell Programming and Scripting

Need last month files after 10th of every month

Hi, I need all file names in a folder which has date >= 10th of last month, Example : files in folder AUTO_F1_20140610.TXT BUTO_F1_20140616.TXT CUTO_F1_20140603.TXT FA_AUTO_06012014.TXT LA_AUTO_06112014.TXT MA_AUTO_06212014.TXT ZA_AUTO_06232014.TXT Output: AUTO_F1_20140610.TXT... (9 Replies)
Discussion started by: nani1984
9 Replies
All times are GMT -4. The time now is 04:44 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy