awk being awkward

Trying to calculate an average and send it to another file so I’m using the command

sed 1d data.dat | awk '{ print $1,"	"$2,"	"$3,"	"$4,"	",$3/$2 }' > test.csv

The first line needs to be removed because it’s a 0 and then awk throws an error and refuses to average.

Awk does it stuff but doesn’t output the data as I want it. It does this

03/09/10 	1 	47 	618
 	 47
03/10/10 	4 	226 	577
 	 56.5
03/11/10 	8 	452 	536
 	 56.5

when I want it to all on one line.
Any ideas why this is?

something to do with the final " " on its own followed immediately by a comma ?

Remove that and its still on anorther line, just not tabbed in.

did you remove the " " or the comma ?

I know nothing but my heart says looking at it the last comma needs removing from the orginal as it seems structured tab&field but last bit its tab & new statement.

Removing the last comma didn’t have any effect :frowning: Nor did removing any of the commas to be honest!

I’ve narrowed it down to adding column $4 which is the end of the row.


sed 1d data.dat | awk '{sum=$3/$2; print $1"	"$2"	"$3"	"sum}' > test.csv
gives
03/09/10	1	47	47
03/10/10	4	226	56.5
03/11/10	8	452	56.5

but


sed 1d data.dat | awk '{sum=$3/$2; print $1"	"$2"	"$3"	"$4"	"sum}' > test.csv
gives
03/09/10	1	47	618
	47
03/10/10	4	226	577
	56.5

Only reason I can think it’s doing that is because $4 somehow has a new line command there (well I guess it does as that’s the last datset on that row) Any ideas?

any chance of a copy/paste of data.dat ?

Sorted it now with the help of everyone :smiley:

awk '$3!=0 || $2!=0{gsub(/\r/,"",$4);print $1"	"$2"	"$3"	"$4"	"$3/$2}' data.dat > test.csv

$4 had a carriage with it (\r not
) - remove that and it disappears and everything’s as it should be.

Ta. Had something similar, worked for me too. Cheers peeps:tiphat: