2

I have two numbers in test.txt file, I would like a way to divide the second number by the first number and assign the result to a variable or file in a bash script that I can use. I have used awk "NR==1" test.txt to get number1 displayed on command line but inside of script, I have tried awk "NR==2" test.txt / awk "NR==1" test.txt and all of the quote / bracket combinations of such. The command line use gives num2/num1 as result, not the result of division or gives syntax error. I am using Debian Linux as OS. test.txt file contains

0250368000
0182885654

I want the second number divided by the first var=0182885654/0250368000 The numbers will change from time to time and I want a script that will process the numbers.

  • Can you show us the text-file with the numbers (add to question). And exactly what you have tried. – ctrl-alt-delor Mar 14 '15 at 18:04
4

Assign to a variable

var=$(awk 'NR==1{a=$0}NR==2{print $0/a}' test.txt)

or output to a file:

awk 'NR==1{a=$0}NR==2{print $0/a}' test.txt > output
  • result is -nan, not really correct answer – Harold Meneley Mar 15 '15 at 2:47
  • 1
    @HaroldMeneley With your example of test.txt file I get 0.730467. Is there anything else in the file apart of these numbers? – jimmij Mar 15 '15 at 9:52
  • There is definitely something else in the file. printf %s\\n 0250368000 0182885654 >test.txt; { cat test.txt; echo /p; } | dc prints 1 not a bunch of dc stack empty errors. You could only get a bunch with a bunch of commands anyway. – mikeserv Mar 15 '15 at 18:09
  • There are two numbers only in plain text format, the first number is the large one, and the second should represent only a portion of the first. The numbers will change every day. There are no character encoding or special formatting.The problem seems to be the simple fact that the answer is less than one, not an integer. – Harold Meneley Mar 16 '15 at 18:53
1
quotient=$(dc -f test.txt -e 'r[num desired precision]k/p')
  • bunch of dc stack empty errors and unimplemented errors – Harold Meneley Mar 15 '15 at 2:51
  • Well, that does depend on the contents of test.txt being only two numbers. Apparently that is not the case. – mikeserv Mar 15 '15 at 3:48
0

I have used an old step by step trick to obtain something that works, but would have liked to do it all in one step.

num1=$(awk 'NR==2' test1.txt)
echo $num1 #result is 0247421924
num2=$(awk 'NR==1' test1.txt)
echo $num2 #result is 0250368000
score=`awk 'BEGIN{printf("%0.0f", '$num1' * 100 / '$num2' )}'`

echo $score # result is 99

As you can see, I had to multiply the first number by 100 to get an integer. Sorry, couldn't get line spacing to work

-1
 awk ' NR == 1 { a = $1 }
       NR == 2 { b = $1 }
       END { print a
             print b
             print b/a } ' data

where data is a text file containing,

0250368000
0182885654
  • This answer actually does obtain the proper answer and I only had to add ; between prints to get it to work on the command line. Thanks. – Harold Meneley Mar 16 '15 at 19:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.