Quantcast
Channel: How to calculate max and min of multiple columns (row wise) using awk - Stack Overflow
Browsing index pages (4 articles)

How to calculate max and min of multiple columns (row wise) using awk

This might be simple - I have a file as below:df.csvcol1,col2,col3,col4,col5A,2,5,7,9B,6,10,2,3C,3,4,6,8I want to perform max(col2,col4) - min(col3,col5) but I get an error using max and min in awk and...

View Article


Answer by Daweo for How to calculate max and min of multiple columns (row...

get an error using max and min in awk and write the result in a new column.No such function are available in awk but for two values you might harness ternary operator, so in place...

View Article


Answer by anubhava for How to calculate max and min of multiple columns (row...

You may use this awk:awk 'BEGIN{FS=OFS=","} NR==1 {print $0, "New_col"; next} {print $0, ($2 > $4 ? $2 : $4) - ($3 < $5 ? $3 : $5)}'...

View Article

Answer by Ed Morton for How to calculate max and min of multiple columns (row...

$ cat tst.awkBEGIN { FS=OFS="," }{ print $0, (NR>1 ? max($2,$4) - min($3,$5) : "New_col") }function max(a,b) {return (a>b ? a : b)}function min(a,b) {return (a<b ? a : b)}$ awk -f tst.awk...

View Article
Browsing index pages (4 articles)


Latest Images