Print last occurrence if first field match

Hi All,

I have an input below. If the term in the 1st column is equal, print the last row which 1st column is equal.In the below example, it's " 0001 k= 27 " and " 0004 k= 6 " (depicted in bold). Those terms in 1st column which are not repetitive are to be printed as well. Can any body help me by using awk or perl ? Thanks

Input:

0001 k= 1
0001 k= 26
0001 k= 27
0002 k= 1
0003 k= 1
0004 k= 1
0004 k= 4
0004 k= 5
0004 k= 6
Output:

0001 k= 27
0002 k= 1
0003 k= 1
0004 k= 6

assuming your input is sorted based on the first column...

nawk -f ray.awk myInputFile

ray.awk:

# if the value in the first column ($1) does not equal the value stored in "prevIDX"
# and we are not at the FIRST record (FNR != 1) [FNR = RecordNumber in the current File]- print the value of "prev"
$1 != prevIDX && FNR != 1 { print prev}

# assign the value of the FIRST field ($1) to a variable "prevIDX"
# assign the entire record/line to the variable "prev"
{ prevIDX=$1; prev=$0 }

# after ALL the records/lines were processed - print the value of the variable "prev" 
# the last/dangling record/line
END { print prev }

Hi vgersh99,

That's cool!! It works!! You a are brillant!!
But i ' m new to the " prev " and " FNR " term.
Can you explain your code so that i can understand better ?

see the comments in the original post.

Hi vgersh99,

Can i use " NR " instead of " FNR " for this case this there's only one input file ?

don't see why not.....

Thanks vgersh99!! :smiley:
Thanks for your guidance!

HI,

code:

awk '
{
out[$1]=$0
}
END{
for (i in out)
print out
}' filename

Hi Summer,
Your code is able to work but some how the sequence of display seems to be jumbled up. " 0004 " should be at the last line but it came to the 1st line.

Below is my output using your code:

$ awk -f myawk data

0004 k= 6
0001 k= 27
0002 k= 1
0003 k= 1

Hi vgersh99,

If i need to capture the 1st occurrence instead that is if column1 of row1 equals to column1 of row2 then the whole line of column1 row1 is being printed out , how can i modify your code below to fulfill this requirement?

$1 != prevIDX && FNR != 1 { print prev}
{ prevIDX=$1; prev=$0 }
END { print prev }

Input:

0001 k= 1
0001 k= 26
0001 k= 27
0002 k= 1
0003 k= 1
0004 k= 1
0004 k= 4
0004 k= 5
0004 k= 6

Output:

0001 k= 1
0002 k= 1
0003 k= 1
0004 k= 1