bash - Subtracting numbers in a column -


inside file have column numbers 10 elements. want subtract 1st 3rd number, 2nd 4th, 3rd 5th, 4th 6th, , on till 8th 10th.

for example:

10.3456 6.3452 11.2456 5.6666 10.5678 6.4568 14.7777 7.5434 16.5467 8.9999 

and file subtraction

3rd-1st 4th-2nd 5th-3rd 6th-4th 7th-5th 8th-6th 9th-7th 10th-8th 

quick , dirty:

$  awk '{a[nr]=0+$0}end{for(i=3;i<=nr;i++)print a[i]-a[i-2]}' file 0.9 -0.6786 -0.6778 0.7902 4.2099 1.0866 1.769 1.4565 

update: came funny way:

$ awk 'nf>1{print $1-$2}' <(paste  <(sed -n '3,$p' file) file)   0.9 -0.6786 -0.6778 0.7902 4.2099 1.0866 1.769 1.4565 

update2, make result csv:

kent$  awk '{a[nr]=0+$0}end{for(i=3;i<=nr;i++)             printf "%s%s", a[i]-a[i-2],nr==i?rs:","}' file 0.9,-0.6786,-0.6778,0.7902,4.2099,1.0866,1.769,1.4565 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -