awk prints only last line

data.txt:

NEWTEXTS="frq=63,std=-0.00533584,time=Mar-21-(09:15:03)-2016,epoch=1458576903,avg=64.2059,212.698
frq=197,std=0.587585,time=Mar-21-(09:16:02)-2016,epoch=1458576962,avg=64.2059,483.756
frq=178,std=0.503514,time=Mar-21-(09:46:02)-2016,epoch=1458578762,avg=64.2059,500
frq=0,std=-0.284097,time=Mar-21-(10:12:01)-2016,epoch=1458580321,avg=64.2059,246
frq=145,std=0.357496,time=Mar-21-(10:13:01)-2016,epoch=1458580381,avg=64.2059,514.483
frq=178,std=0.503514,time=Mar-21-(10:16:01)-2016,epoch=1458580561,avg=64.2059,510.112
frq=84,std=0.0875845,time=Mar-21-(10:46:02)-2016,epoch=1458582362,avg=64.2059,1098.81
frq=63,std=-0.00533584,time=Mar-21-(10:55:02)-2016,epoch=1458582902,avg=64.2059,1192.06
frq=180,std=0.512363,time=Mar-21-(11:15:02)-2016,epoch=1458584102,avg=64.2059,528.333
frq=63,std=-0.00533584,time=Mar-21-(11:34:03)-2016,epoch=1458585243,avg=64.2059,422.222
frq=329,std=1.17166,time=Mar-21-(11:35:10)-2016,epoch=1458585310,avg=64.2059,237.082
frq=63,std=-0.00533584,time=Mar-21-(11:45:03)-2016,epoch=1458585903,avg=64.2059,252.381
frq=222,std=0.698204,time=Mar-21-(11:46:01)-2016,epoch=1458585961,avg=64.2059,470.721
frq=3,std=-0.270823,time=Mar-21-(12:11:01)-2016,epoch=1458587461,avg=64.2059,4800
frq=147,std=0.366346,time=Mar-21-(12:12:01)-2016,epoch=1458587521,avg=64.2059,586.395
frq=182,std=0.521213,time=Mar-21-(12:16:01)-2016,epoch=1458587761,avg=64.2059,491.209
frq=179,std=0.507938,time=Mar-21-(12:31:02)-2016,epoch=1458588662,avg=64.2059,565.363"

awk code:

echo "${NEWTEXTS}" | awk -F, '{a=$1; b=$2; c=$3; d=$4; e=$5; f=$6 } END{printf("%s,%s,%s,%s,%s,%0.f\n", a,b,c,d,e,f)}'

this awk code only prints the last line. but i want it to print all the lines but with the last field of each line rounded up.

any ideas?

Hello SkySmart,

Could you please try following and let me know if this helps.

echo "${NEWTEXTS}" | awk -F, '{a=$1; b=$2; c=$3; d=$4; e=$5; f=$6 } {printf("%s,%s,%s,%s,%s,%0.f\n", a,b,c,d,e,f)}'

Off course culprit there was END in your code. Also you could go through the manual page of awk by doing man awk , as follows mentioned in that for END .

Hope this helps.

Thanks,
R. Singh

1 Like

Your suggestion works!

Thank you!

As per the requirements, your current solution does not always round up the last field. The below solution will always round up the last field:

echo "${NEWTEXTS}" | awk '$NF=$NF==int($NF)?int($NF):int($NF)+1' OFS=, FS=,
1 Like

it works. thank you!
care to explain how the awk is working?

Sure,

So this part below will re-evaluate the value of $NF for every line read. The value of $NF being the last field.

awk '$NF=

For this part, ideally it should be enclosed in brackets. This is what the value of $NF is being set to. It is using the conditional (ternary) operators ?: to set the value of $NF to int($NF) if $NF is already equal to int($NF), so basically if it is already an integer and not a decimal, don't change it. If $NF is not equal to int($NF), i.e. Is a decimal, then set $NF to int($NF)+1. The int function is essentially rounding down the number.

$NF==int($NF)?int($NF):int($NF)+1'

This will set both the field separator (FS), same as doing - F, and the output field separator (OFS) to a comma.

OFS=, FS=,

And lastly, awk will perform it's default action of printing the lines it performs the operation on.

1 Like