We help IT Professionals succeed at work.

How to add variable string in unix

welcome 123
welcome 123 asked
on
319 Views
Last Modified: 2014-05-22
I have string A which has value dynamical comming form database.
I want to add that string + 1 and get the output.

For example:
A=05
I want add this variable +1.
B=$A+1
and desired outpu should be 05+1= 06.
Output should be 06.

Note I am using ksh script.

Can you please reply as soon as possible.

Thanks,
Sailaja.
Comment
Watch Question

Author

Commented:
I beleve dc operator can be used.

Author

Commented:
I am geting the result as 6 I want the result as 06 how we get that.

BD0_MON=05
val=`expr $BD0_MON + 1`
6
desired output 06
CERTIFIED EXPERT
Most Valuable Expert 2013
Top Expert 2013

Commented:
B=$(echo $A | awk '{printf "%02d", $1+1}')

Or like this:

B=$(awk -v A="$A" 'BEGIN {printf "%02d", A+1}')

Under bash this works as well:

B=$(awk '{printf "%02d", $1+1}' <<<$A)
CERTIFIED EXPERT
Most Valuable Expert 2013
Top Expert 2013

Commented:
If it's for calculating months so that you want 12 + 1 to become 01 try this:

B=$(echo $A | awk '{printf "%02d", ($1+1)%12}')

B=$(awk '{printf "%02d", ($1+1)%12}' <<<$A)  # BASH

B=$(awk -v A="$A" 'BEGIN {printf "%02d", (A+1)%12}')
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Most Valuable Expert 2013
Top Expert 2013

Commented:
Sorry, ozo, my arithmetic skills are indeed quite rusty, but I can't see the difference.

Example "7":

My version: 7 + 1 gives 8. Dividing 8 by 12  gives remainder 8.

Your version: Dividing 7 by 12 gives remainder 7. 7 + 1 gives 8.

Example "12":

My version:  12 + 1 gives 13. Dividing 13 by 12 gives remainder 1.

Your version: Dividing 12 by 12 gives remainder 0. 0 + 1 gives 1.


(m+1)%n = m%n+1

OK, your version saves a pair of parentheses.

Confused:

wmp
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
Example, "11"
CERTIFIED EXPERT
Most Valuable Expert 2013
Top Expert 2013

Commented:
I saw that coming  ...

Thanks for the lesson!

wmp
CERTIFIED EXPERT
Most Valuable Expert 2013
Top Expert 2013
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.