bash awk extract and sum values from one numeric col -


i have data in file:

$ cat data.txt facture-2817806.txt total ttc 27.11 € facture-2857125.txt total ttc 37.92 € facture-2895824.txt total ttc 43.61 € facture-2922275.txt total ttc 46.73 € facture-2935969.txt total ttc  8.29 € facture-2977623.txt total ttc 41.79 € facture-3005553.txt total ttc 46.18 € facture-3020047.txt total ttc 20.34 € facture-3061124.txt total ttc 28.22 € facture-3097529.txt total ttc 59.65 € facture-3258989.txt total ttc 29.31 € facture-3637195.txt total ttc 11.17 € facture-3681794.txt total ttc 44.52 € facture-3726992.txt total ttc 28.20 € facture-3752273.txt total ttc 42.15 € facture-3770970.txt total ttc 24.01 € 

i want sum float value of 1 col, extract column:

$ cat data.txt | awk '{print $4}' 27.11 37.92 43.61 46.73 8.29 41.79 46.18 20.34 28.22 59.65 29.31 11.17 44.52 28.20 42.15 24.01 $ 

what bash way sum of 1 more pipe '|' ?

$ cat data.txt | awk '{print $4}' | xxx 

note: there similar issue here: why awk refuse sum floats in file float numbers not alone. additional liner after pipe.

i want sum float value of 1 col

the idiomatic way

awk '{count+=$4}end{printf "total : %f\n",count}' your_file 

the hard way

awk 'begin{count=0}{count+=$4}end{printf "total : %f\n",count}' your_file 

as side note, if wish specify number of digits after decimal point use %0.2f.

note: don't need pipe @ all.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -