Adding Two Number With Expression Function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding Two Number With Expression Function
# 1  
Old 08-29-2009
Adding Two Number With Expression Function

ai,

i have one question about shell script regarding for "expr" function

i have using this command "previous=`expr $previous + 1`" where previous value is 0001, but when the script running, the result appear as

expr 1 + 1 = 2 not 0002. The result i need as expr 0001 + 0001 should be 0002...

how to i get the 0002 result...any trick or changes using expr function?
# 2  
Old 08-29-2009
Code:
$ 
$ x=0001
$ expr $x + 1 | awk '{printf "%04d\n", $0}'
0002
$ 
$ # or
$ expr $x + 1 | xargs printf "%04d\n"
0002
$

tyler_durden
# 3  
Old 08-30-2009
Quote:
Originally Posted by dinodegil
ai,

i have one question about shell script regarding for "expr" function

expr is not a funtion. It is an external command for which there is no use in a POSIX shell.
Quote:

i have using this command "previous=`expr $previous + 1`" where previous value is 0001,

0001 is not an integer; at best it is an octal number.
Quote:
but when the script running, the result appear as

expr 1 + 1 = 2 not 0002. The result i need as expr 0001 + 0001 should be 0002...

how to i get the 0002 result...any trick or changes using expr function?

First, do not use expr, use the shell's arithemetic:

Code:
echo $(( 0001 + 0001 ))

I you need it to be padded with zeroes, use printf:

Code:
printf "%04d\n" $(( 0001 + 0001 ))

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding number in one column

Hello I have something like this a 1 b 1 c 1 d 1 e 1 This is inside 1.dat and this is what I am trying to get a 2 b 2 c 2 d 2 e 2 (4 Replies)
Discussion started by: jeo_fb
4 Replies

2. Shell Programming and Scripting

Regular expression for 6 digit number present in a line

Hello Team, i have a file test1.txt, in which i have to grep only the 6 digit number from it, Could you pls help in this. $cat test1.txt <description>R_XYZ_1.6 r370956</description> $ grep "\{6\}" test1.txt <description>R_XYZ_1.6 r370956</description> i need output as 370956. ... (3 Replies)
Discussion started by: chandana hs
3 Replies

3. Shell Programming and Scripting

adding a number with sed or awk.

Hi.. I have this delicate problem..:wall: I have this huge ldif file with entry's like this example below.. And I need to change the following entrys. telephoneNumber: emNotifNumber: billingnumber= BillingNumber: Al these entrys has a number like 012345678 and it needs to add one more... (15 Replies)
Discussion started by: pelama
15 Replies

4. Shell Programming and Scripting

Adding number before file extension

Hi , I have a file which has a line starts with $segment_name and has the below data source data $Segment_Name = 123.ABC.123.01.txt $Segment_Name = 123.ABC.ABC.txt $Segment_Name = 123.ABC.12A3.txtMy target data should be $Segment_Name = 123.ABC.123.01.txt $Segment_Name =... (2 Replies)
Discussion started by: shruthidwh
2 Replies

5. UNIX for Advanced & Expert Users

regular expression for split function in perl

Hi, Below is an example of a record I have, which I wish to split using the perl's split function and load it into an array. I am having tough time figuring out the exact reg-ex to perform the split. Given record: "a","xyz",0,2,48,"abcd","lmno,pqrR, stv",300,"abc",20, The delimiter to... (4 Replies)
Discussion started by: jghoshal
4 Replies

6. Shell Programming and Scripting

awk conditional expression to compare field number and variable value

Hi, I'm trying to compare the value in a field to the value in a variable using awk. This works: awk '$7 == "101"'but this is what I want (and it doesn't work): value=101 awk '$7 == "$value"' Any help or insight on this would be great. Thanks in advance. (1 Reply)
Discussion started by: goodbenito
1 Replies

7. Shell Programming and Scripting

Perl - Grep function regular expression

For some reason, @logs is a list of log files @filter is a list of expressions to grep out foreach (@logs){ open READ, "<$_" or die $!; @temp=<READ>; close READ; foreach (@filter){ print grep /$_/,@temp ; } } returns a regex error in one of the files... (4 Replies)
Discussion started by: adelsin
4 Replies

8. Shell Programming and Scripting

regular expression grepping lines with VARIOUS number of blanks

Hi, I need a regular expression grepping all lines starting with '*' followed by a VARIOUS number of blanks and then followed by the string 'Runjob=1'. I tried that code, but it doesn't work: grep -i '*'+'Runjob=1' INPUT_FILE >>OUTPUT_FILE Can someone help me? Thanks (8 Replies)
Discussion started by: ABE2202
8 Replies

9. Shell Programming and Scripting

Adding a sequence number within a file

Can someone please help. I need to add a sequence number to the start of each line of a file. It needs to be 6 characters long. ie 000001 next line starts 000002 etc. (4 Replies)
Discussion started by: Dolph
4 Replies

10. Shell Programming and Scripting

Adding a columnfrom a specifit line number to a specific line number

Hi, I have a huge file & I want to add a specific text in column. But I want to add this text from a specific line number to a specific line number & another text in to another range of line numbers. To be more specific: lets say my file has 1000 lines & 4 Columns. I want to add text "Hello"... (2 Replies)
Discussion started by: Ezy
2 Replies
Login or Register to Ask a Question