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
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 consists of columns. I tried below codes but all values look Inf.
nawk -F, '{print 1ドル*100/2ドル}' data > results
-
please provide a sample of input file and a sample of output file.Archemar– Archemar2015年12月19日 10:16:58 +00:00Commented Dec 19, 2015 at 10:16
-
input= 100 150;200 250; two columns and two rows, output=67;80, two columnsdeepblue_86– deepblue_862015年12月19日 10:18:45 +00:00Commented Dec 19, 2015 at 10:18
-
Please do not respond in comments; edit your question to make it clearer and more complete. Why do you expect your output file to have two columns when you are producing only one result per line of input? Are you expecting all the results (from all the lines of the input file) to be written to a single line of the output file?Scott - Слава Україні– Scott - Слава Україні2015年12月19日 11:36:51 +00:00Commented Dec 19, 2015 at 11:36
1 Answer 1
You’re saying -F,
but (according to your comment)
your data are separated by spaces, not commas. So this is what [n]awk
sees
Line 1: 1ドル="100 150" 2ドル=""
Line 2: 1ドル="200 250" 2ドル=""
Once you use these in an arithmetic expression, awk
converts them to
Line 1: 1ドル=100 2ドル=0
Line 2: 1ドル=200 2ドル=0
and so you get division by 0. Just leave off the -F,
(or change your data to be comma-separated)
and you should get the output you want.
Well, almost; on my system, I get 66.6667
and 80
.
You just need to round the result to the nearest integer
(if that is, in fact, what you want).