HollowBastion
HollowBastion

Reputation: 223

awk output first two columns then the minimum value out of the third and fourth columns

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..

Upvotes: 0

Views: 562

Answers (1)

karakfa
karakfa

Reputation: 67467

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

Upvotes: 3

Related Questions