victorhernandezzero
victorhernandezzero

Reputation: 123

How I could get the last value per record in awk?

Im triying to get the last value per record but I dont know how

This is my input

grey;5
grey;6
grey;3
blue:2
blue;1
blue;0
red;5
red;7
red;2

I need to get this

grey;3
blue;0
red;2

Please help me with this

Upvotes: 0

Views: 64

Answers (2)

James Brown
James Brown

Reputation: 37414

I just replaced the : with a ;:

$ awk -F\; '{a[$1]=$0} END{for(i in a) print a[i]}' file
red;2
grey;3
blue;0

Explained: Hash everything based on the $1. In this case last come, last served or smth.

Upvotes: 2

RavinderSingh13
RavinderSingh13

Reputation: 133590

try:

awk -F'[:;]' 'FNR==NR{A[$1]=$0;next} ($1 in A){print A[$1];delete A[$1]}'   Input_file  Input_file

Upvotes: 2

Related Questions