awk output first two columns then the minimum valu

2019-09-08 06:00发布

I have a tab delimited file like so:

col1 col2 col3 col4
a 5 y:3.2 z:5.1
b 7 r:4.1 t:2.2
c 8 e:9.1 u:3.2
d 10 o:5.2 w:1.1

For each row, I want to output the values in the first and second columns, and the smallest number out of the two values in the third and fourth columns.

col1 col2 min
a 5 3.2
b 7 2.2
c 8 3.2
d 10 1.1

My poor attempt:

awk -F'\t' '{min = ($3 < $4) ? $3 : $4; print $1, $2, min}'

One reason it's incorrect is because the values in the third and fourth columns aren't numbers but strings. I don't know how to extract the number out of the third and fourth columns, the number is always after the colon..

标签: awk
1条回答
放我归山
2楼-- · 2019-09-08 06:22

awk to the rescue!

$ awk -F'[ *:]' 'NR==1{print $1,$2,"min";next} {print $1,$2, $4<$6?$4:$6}' file

col1 col2 min
a 5 3.2
b 7 2.2
c 8 3.2
d 10 1.1
查看更多
登录 后发表回答