-2

I have a data file which consists of two columns. I need to perform arithmetic operation with these columns as follows;

column_1=a(i) column_2=b(i), where i is the line number.

Calculations:

result_1=a(1)*100/b(1)

result_2=a(2)*100/b(2)

Then I need to write all results into another text file, consisting of columns. I tried below codes but all values look Inf.

nawk -F, '{print $1*100/$2}' data > results
3
  • No idea, what do you mean. Your awk command writes one column with the result for each line. Where is the problem?
    – A.B.
    Dec 19, 2015 at 10:46
  • Well , how does the actual data look like ? Provide a couple of example lines to work with. Dec 19, 2015 at 11:05
  • @serg, example data= 100 150; 200 2500. two columns and two rows Dec 19, 2015 at 11:10

1 Answer 1

1

Here's the code that you've said works, as discussed in the comments

xieerqi:
$ awk '{ print "a="$1" b="$2" result="($1*100)/$2   }' input.txt               
a=100 b=150 result=66.6667
a=200 b=2500 result=8

xieerqi:
$ cat inpu
input-10-       input.txt       inputData.txt   inputFile.txt   
$ cat input.txt                                                                
100 150
200 2500

You've also mentioned in the comments that last two lines in the output look like

a= b= result=-nan 
a= b= result=-nan

There is possibility your file contains two extra lines with spaces or non-printable characters. Run cat -nA input.txt to print the file with line numbers and show hidden characters

$ cat -nA input.txt                                                            
     1  100 150$
     2  200 2500

But overall this side issue is beyond the scope of what you've originally asked

Not the answer you're looking for? Browse other questions tagged .