# Print last occurrence if first field match

**URL:** <https://community.unix.com/t/print-last-occurrence-if-first-field-match/177807>\
**Category:** Shell Programming and Scripting\
**Created:** [November 15, 2007, 9:30am UTC](https://community.unix.com/t/print-last-occurrence-if-first-field-match/177807 "2007-11-15T09:30:44Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raynon](https://community.unix.com/letter_avatar/raynon/32/5_5575768a8748004e209b776fc1b2916d.png) [@Raynon](https://community.unix.com/u/Raynon)\
**Post date:** [November 15, 2007, 9:30am UTC](https://community.unix.com/t/print-last-occurrence-if-first-field-match/177807/1 "2007-11-15T09:30:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [November 15, 2007, 9:50am UTC](https://community.unix.com/t/print-last-occurrence-if-first-field-match/177807/2 "2007-11-15T09:50:23Z")

</div>

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

nawk -f ray.awk myInputFile

ray.awk:

```nohighlight
# 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 }

```

---

<div class="post-metadata">

**Author:** ![Raynon](https://community.unix.com/letter_avatar/raynon/32/5_5575768a8748004e209b776fc1b2916d.png) [@Raynon](https://community.unix.com/u/Raynon)\
**Post date:** [November 15, 2007, 10:05am UTC](https://community.unix.com/t/print-last-occurrence-if-first-field-match/177807/3 "2007-11-15T10:05:40Z")

</div>

> [@vgersh99](#):
>
> assuming your input is sorted based on the first column...
> 
> nawk -f ray.awk myInputFile
> 
> ray.awk:
> 
> ```plaintext
> $1 != prevIDX && FNR != 1 { print prev}
> { prevIDX=$1; prev=$0 }
> 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 ?

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [November 15, 2007, 10:17am UTC](https://community.unix.com/t/print-last-occurrence-if-first-field-match/177807/4 "2007-11-15T10:17:52Z")

</div>

see the comments in the original post.

---

<div class="post-metadata">

**Author:** ![Raynon](https://community.unix.com/letter_avatar/raynon/32/5_5575768a8748004e209b776fc1b2916d.png) [@Raynon](https://community.unix.com/u/Raynon)\
**Post date:** [November 16, 2007, 3:07am UTC](https://community.unix.com/t/print-last-occurrence-if-first-field-match/177807/5 "2007-11-16T03:07:38Z")

</div>

Hi vgersh99,

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

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [November 16, 2007, 8:41am UTC](https://community.unix.com/t/print-last-occurrence-if-first-field-match/177807/6 "2007-11-16T08:41:09Z")

</div>

> [@raynon](#):
>
> Hi vgersh99,
> 
> Can i use " NR " instead of " FNR " for this case this there's only one input file ?

don't see why not.....

---

<div class="post-metadata">

**Author:** ![Raynon](https://community.unix.com/letter_avatar/raynon/32/5_5575768a8748004e209b776fc1b2916d.png) [@Raynon](https://community.unix.com/u/Raynon)\
**Post date:** [November 16, 2007, 8:59am UTC](https://community.unix.com/t/print-last-occurrence-if-first-field-match/177807/7 "2007-11-16T08:59:38Z")

</div>

Thanks vgersh99!! 😃  
Thanks for your guidance!

---

<div class="post-metadata">

**Author:** ![summer\_cherry](https://community.unix.com/letter_avatar/summer_cherry/32/5_5575768a8748004e209b776fc1b2916d.png) [@summer\_cherry](https://community.unix.com/u/summer_cherry)\
**Post date:** [November 18, 2007, 9:49pm UTC](https://community.unix.com/t/print-last-occurrence-if-first-field-match/177807/8 "2007-11-18T21:49:17Z")

</div>

HI,

code:

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

```

---

<div class="post-metadata">

**Author:** ![Raynon](https://community.unix.com/letter_avatar/raynon/32/5_5575768a8748004e209b776fc1b2916d.png) [@Raynon](https://community.unix.com/u/Raynon)\
**Post date:** [November 19, 2007, 8:40am UTC](https://community.unix.com/t/print-last-occurrence-if-first-field-match/177807/9 "2007-11-19T08:40:20Z")

</div>

> [@summer\_cherry](#):
>
> HI,
> 
> code:
> 
> ```plaintext
> 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

---

<div class="post-metadata">

**Author:** ![Raynon](https://community.unix.com/letter_avatar/raynon/32/5_5575768a8748004e209b776fc1b2916d.png) [@Raynon](https://community.unix.com/u/Raynon)\
**Post date:** [March 13, 2008, 3:28am UTC](https://community.unix.com/t/print-last-occurrence-if-first-field-match/177807/10 "2008-03-13T03:28:20Z")

</div>

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?

```nohighlight
$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
