Please help - Command to Subtract two numbers without losing prefix zeros


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Please help - Command to Subtract two numbers without losing prefix zeros
# 1  
Old 08-02-2017
Please help - Command to Subtract two numbers without losing prefix zeros

Hello,

I have a variable LOGNUM with values 0000095, When i subtract the variable by 1, Its losing its leading zeros. Can you please help me here ?

Code:
LOGNUM=0000095
$OLDLOG=`echo "${LOGNUM}-1"|bc`
$ echo $OLDLOG
94


Am expecting output as
Code:
0000094



Appreciate your help!

Thanks,
Prince

Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 08-02-2017 at 05:49 PM.. Reason: Added CODE tags.
# 2  
Old 08-02-2017
Code:
LOGNUM=0000095
printf "%0${#LOGNUM}d\n" $(echo "$LOGNUM-1" | bc)


Last edited by rdrtx1; 08-02-2017 at 05:53 PM..
This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 08-02-2017
Does it have to be bc? Try
Code:
awk -vLNUM=$LOGNUM 'BEGIN {printf "%07d\n", LNUM-1}'
0000094

This User Gave Thanks to RudiC For This Post:
# 4  
Old 08-02-2017
When you post questions like this in the future, please tell us what operating system and shell you're using!

If you're using ksh, the easy way to do it is:
Code:
LOGNUM=0000095
OLDLOG=$(printf "%07d" $((LOGNUM - 1)))
echo $OLDLOG

If you're using bash, you could use:
Code:
LOGNUM=0000095
OLDLOG=$(printf "%07d" $(echo "$LOGNUM - 1" | bc))
echo $OLDLOG

You can't use arithmetic expansions for this with bash because bash treats all numbers with a leading 0 as octal (not decimal) values.

If you're using a pure Bourne shell, you could use:
Code:
LOGNUM=0000095
OLDLOG=`echo "$LOGNUM - 1" | bc`
OLDLOG=`printf "%07d" $OLDLOG`
echo $OLDLOG

One of the above is likely to work with any shell that is based on Bourne shell syntax. I make no claims about how to do this with a shell based on csh syntax.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 08-02-2017
With bash, try
Code:
printf "%0${#LOGNUM}d\n" "$(( 10#$LOGNUM - 1 ))"
0000094

This User Gave Thanks to RudiC For This Post:
# 6  
Old 08-02-2017
Hello,

It works if number is below 0000099, If its above 0000100, it is not working as expected.

Code:
$ LOGNUM=0000120
$ printf "%0${#LOGNUM}d\n" "(( $LOGNUM - 1 ))"
0000079

Thanks,

Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 08-02-2017 at 06:06 PM.. Reason: Added CODE tags.
# 7  
Old 08-02-2017
See updated post #2.
This User Gave Thanks to rdrtx1 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Remove trailing zeros from numbers

Hi, I am trying to remove trailing zeros from numbers in a csv file. CSV Input : 0.5000,abc,2.00,2.400,285.850,285a.850,205.180800,mno000,a0b0,2.860 Expected Output : .5,abc,2,2.4,285.85,285a.850,205.1808,mno000,a0b0,2.86 Can you please help. Thanks. (11 Replies)
Discussion started by: manubatham20
11 Replies

2. Shell Programming and Scripting

Extract Uniq prefix from a start and end prefix

Dear All, assume i have a file with content: <Start>6000</Start> <Stop>7599</Stop> the output is: 6000 7000 7100 7200 7300 7400 7599 how should we use any awk, sed, perl can do this task, means to extract the uniq prefixes from the start and stop prefix. Thanks Jimmy (3 Replies)
Discussion started by: jimmy_y
3 Replies

3. Shell Programming and Scripting

Losing carriage return (X0D) after running awk command

Hi Forum. I'm running the following awk command to extract the suffix value (pos 38) from the "AM00" record and append to the end of the "AM01" record. awk 'substr($0,13,4)=="AM00" {SUFFIX = substr($0,38,2)} substr($0,13,4)=="AM01" {$0 = $0 SUFFIX} 1' before.txt > after.txt Before.txt:... (2 Replies)
Discussion started by: pchang
2 Replies

4. Shell Programming and Scripting

Numbers with leading zeros

Hi, i have a variable which conatins values like 00001,0003,00067,00459. I want to use the values one by one and in the same form as they are like 00001,0003,00067,00459. Also can anyone tell me how to increment those numbers by 1,keeping the format as same like 00002,0004,00068,00460.... (5 Replies)
Discussion started by: arijitsaha
5 Replies

5. Shell Programming and Scripting

Fill missing numbers in second column with zeros

Hi All, I have 100 files with names like this: 1.dat, 2.dat, 3.dat until 100.dat. My dat files look like this: 42323 0 438939 1 434 0 0.9383 3434 120.23 3 234 As you can see in the second column, some numbers are missing. I want to fill those missing places with 0's in all... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

6. Shell Programming and Scripting

appending zeros to numbers using awk

hi i want to append zeros to a given number ( varying digits). the total length of the output should be 10 digits. For example: 1)input is var=347 output should be NewVar=0000000347 2) input is var=123456 output should be NewVar=0000123456 i am able to acheive this using typeset... (1 Reply)
Discussion started by: somi2yoga
1 Replies

7. UNIX for Dummies Questions & Answers

Split Function Prefix Numbers

Hello, Hello, I use the following command to split a file: split -Number_of_Lines Input_File MyPrefix_ output is MyPrefix_a MyPrefix_b MyPrefix_c ...... Instead, how can I get numerical values like: MyPrefix_1 MyPrefix_2 MyPrefix_3 ...... (2 Replies)
Discussion started by: Gussifinknottle
2 Replies

8. UNIX for Dummies Questions & Answers

Losing Command History from Prior Sessions

We are running AIX. Shell is ksh. I used to have the ability to recall commands from prior sessions using <exc> k. Now, suddenly, I only can recall commands run during the current session. When I start a session, I have no command history. Also, when I run the shell command from a current... (1 Reply)
Discussion started by: boylepeterj
1 Replies

9. Shell Programming and Scripting

Prefix integer with zeros

I want an integer to be prefix with some zeros so that it makes the integer 5 digit always: For eg: if the digit is 8 i need 00008 if the digit is 8976 ans sud be 08976. Could anyone say simple and efficient commands to this in ksh. Thanks a lot... (4 Replies)
Discussion started by: PRKS
4 Replies

10. Shell Programming and Scripting

AWK solution to subtract multiple columns of numbers

Hope somebody is happy. NR==1 { num_columns=split( $0, menuend ); next; } { split( $0, substrend ); for ( i=1; i<=NF; i++ ) { minuend -= substrend; } } END { print "Result:"; for ( i=1; i<=num_columns; i++ ) { printf(... (3 Replies)
Discussion started by: awkward
3 Replies
Login or Register to Ask a Question