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
# 8  
Old 08-02-2017
Thanks, This works Smilie

---------- Post updated at 01:35 AM ---------- Previous update was at 01:34 AM ----------

Thanks this works fine Smilie
# 9  
Old 08-02-2017
Quote:
Originally Posted by prince1987
Hello,

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

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

Thanks,
I doubt that it is working for all numbers below 99. And, you haven't told us what shell you're using to get these results. The above output is exactly what I would expect if you're using bash as your shell. As I explained in post #4 in this thread, bash treats 0120 (an octal number) as 80 (decimal).
This User Gave Thanks to Don Cragun For This Post:
# 10  
Old 08-02-2017
Thanks a lot. This works Smilie
# 11  
Old 08-03-2017
If you are in ksh (I don't think you ever said) you may be able to use typeset like this:-
Code:
typeset -Z7 LOGNUM
LOGNUM=95
((LOGNUM=$LOGNUM-1))
echo "$LOGNUM"

I get the output 0000094

I don't think this is available in bash, which is a little frustrating because it is so easy, however I'm sure there is a good reason. I have use this sort of thing frequently, so it would be know if there is a danger with it, hence why it has not made it into bash. If there is a neat way like this, I'd love to know it!



Kind regards,
Robin
# 12  
Old 08-03-2017
Hi.

Also zsh, for a file z7:
Code:
zsh <<'EOF'
typeset -Z7 LOGNUM
LOGNUM=95
((LOGNUM=$LOGNUM-1))
echo "$LOGNUM"
EOF

producing:
Code:
$ ./z7
0000094

On a system like:
Code:
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.8 (jessie) 
zsh 5.0.7

Best wishes ... cheers, drl
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