addition with exact format


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers addition with exact format
# 1  
Old 04-21-2010
addition with exact format

Hi,
I need to add two numbers with prefix with some zero, And result should come with the prefix and numbers.
Code:
$a=00000054
$b=`expr  $a + 4 `
$echo $b  # 00000058 <-- Result

How can i get the result with prefix with zero's.
Thanks
Posix
# 2  
Old 04-21-2010
It depends on the shell you're using,
with all shells you could use printf:

Code:
% a=00000054 b="$(($a + 4))"
% printf '%08d\n' "$b"
00000058

With some shells you could define a variable zero padded,
this is ksh93:

Code:
$ typeset -Z8 a b
$ a=00000054 b="$((a + 4))"
$ print $b
00000058


Last edited by radoulov; 04-21-2010 at 02:19 PM..
# 3  
Old 04-21-2010
[QUOTE=radoulov;302414932]It depends on the shell you're using,
with all shell you could use printf:
Code:
% a=00000054 b="$(($a + 4))"
% printf '%08d\n' "$b"
00000058

Strange Out put
Code:
[root@xx]# echo $SHELL
/bin/bash
[root@xx]# a=00000054 b="$(($a + 4))"
[root@xx]# printf '%08d\n' "$b"
00000048

Can't we do zero padding like ksh in bash.
# 4  
Old 04-21-2010
Quote:
Originally Posted by posix
[...]
Strange Out put
Code:
[root@xx]# echo $SHELL
/bin/bash
[root@xx]# a=00000054 b="$(($a + 4))"
[root@xx]# printf '%08d\n' "$b"
00000048

Can't we do zero padding like ksh in bash.
Yes,
your shell is interpreting the number with leading 0's as octal.
Could you please post the output from the following command:

Code:
ps -p$$

P.S. $SHELL contains your login shell, not your current shell.
# 5  
Old 04-22-2010
Hi..out put of command ps -p$$
Code:
[root@xx]# ps -p$$
  PID TTY          TIME CMD
27035 pts/0    00:00:00 bash

# 6  
Old 04-22-2010
OK,
with bash you can force decimal interpretation:

Code:
$ a=00000054 b="$((10#$a + 4))"
$ printf '%08d\n' "$b"

# 7  
Old 04-23-2010
Thanks very much Radoulov. i got to know lots of things about the padding and printf functionality..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

echo exact xml tag from an exact file

Im stumped on this one. Id like to echo into a .txt file all names for an xml feed in a huge folder. Can that be done?? Id need to echo <name>This name</name> in client.xml files. $path="/mnt/windows/path" echo 'recording names' cd "$path" for names in $path than Im stuck on... (2 Replies)
Discussion started by: graphicsman
2 Replies

2. Shell Programming and Scripting

Array value addition

Hello all I have a statement : ARRAY_MOUNT_POINT_NAME=`df -h | awk '{print $6}'| head -`expr $i+2` |tail -1` when the value of i=0 , I want the head argument to be at -2 . Using the expr statement isnt working. Help ! (5 Replies)
Discussion started by: Junaid Subhani
5 Replies

3. Shell Programming and Scripting

QUESTION1: grep only exact string. QUESTION2: find and replace only exact value with sed

QUESTION1: How do you grep only an exact string. I am using Solaris10 and do not have any GNU products installed. Contents of car.txt CAR1_KEY0 CAR1_KEY1 CAR2_KEY0 CAR2_KEY1 CAR1_KEY10 CURRENT COMMAND LINE: WHERE VARIABLE CAR_NUMBER=1 AND KEY_NUMBER=1 grep... (1 Reply)
Discussion started by: thibodc
1 Replies

4. Shell Programming and Scripting

addition of decimal no

a=10.00 pattern=-11.00 b=`echo "$a $pattern" | awk ' printf("%d\n", $1 + $2)'` echo $b not working, also trined bc ,dc but thats not on my m/c. also expr not supporting. any clue? (6 Replies)
Discussion started by: saluja.deepak
6 Replies

5. UNIX for Dummies Questions & Answers

diff with exact difference format

Hi when we diff of 2 files then output is showing lines which are different and then we have to manually find which word is different diff file1 file2 is there any way to show or highlight ONLY the words which are different via diff or some other command ? Thanks rel (1 Reply)
Discussion started by: reldb
1 Replies

6. Shell Programming and Scripting

Addition Of New Fields

Hi, I want to add 3 new fields in the existing file.Please find the example below. input: UID: ABCD UNAME: XYZ Desired Output Tmiestamp: 20101208 UID: ABCD UNAME: XYZ DEPTNO:40 ModifyTImestamp:20101209 If you see the above i have added the 3 columns manually in the output section... (2 Replies)
Discussion started by: Nani7574
2 Replies

7. Shell Programming and Scripting

Addition

Hi all, I am very new to shell programming and trying to learn out the basics. I tried this: $ echo `expr 20 + 30` and it worked. But when i tried this,it does not work. $ a=20 $ b=30 $ echo `expr a + b` The error is: expr: non-numeric argument I cant understand why its... (3 Replies)
Discussion started by: gautamshaw
3 Replies

8. Shell Programming and Scripting

Help with addition

Hi all, I am getting following output by using commands like sort, uniq and awk to the standard output. 110 d 40 a 59 c 9 b 3 e Now at the end I would like to add all the numbers in column 1 and display the count of all numbers i.e. (110 + 40 + 59 + 9 + 3). Also the output may... (3 Replies)
Discussion started by: tenderfoot
3 Replies

9. Shell Programming and Scripting

addition

Hi all, I am new to perl. I need help adding bunch of numbers. I have a file look like this: 1 1 2 1 2 3 1 2 3 4 1 (2 Replies)
Discussion started by: email-lalit
2 Replies

10. Shell Programming and Scripting

Addition problem

Hello Seniors!!! I am trying to add to lines on a file which is delimited by character "|". input.txt Desired Output file should give simple addition. output.txt Can some one provide me simple awk solution to this. M struggling to solve this using awk & for loop inside awk. Thanks... (2 Replies)
Discussion started by: onlyroshni
2 Replies
Login or Register to Ask a Question