Extract only first occurance

Hi All,

From the below file. I need to get only the first occurrence and print. I tried to do it in separate grep not coming as expected

Original file

11001;1213;304;;;;;;;111020677.64;;;;;;;;;;;;;;;;;;;;;;;;;;
11001;1214;304;;;;;;;102376462.96;;;;;;;;;;;;;;;;;;;;;;;;;;
11001;1215;304;;;;;;;12348678.19;;;;;;;;;;;;;;;;;;;;;;;;;;
11001;1216;304;;;;;;;88340.57;;;;;;;;;;;;;;;;;;;;;;;;;;
11001;1217;304;;;;;;;127834.48;;;;;;;;;;;;;;;;;;;;;;;;;;
11001;1218;304;;;;;;;105220.65;;;;;;;;;;;;;;;;;;;;;;;;;;
11002;1213;304;;;;;;;244189.32;;;;;;;;;;;;;;;;;;;;;;;;;;
11002;1214;304;;;;;;;220748.79;;;;;;;;;;;;;;;;;;;;;;;;;;
11003;1215;304;;;;;;;64361.46;;;;;;;;;;;;;;;;;;;;;;;;;;
11003;1216;304;;;;;;;113.89;;;;;;;;;;;;;;;;;;;;;;;;;;
11003;1218;304;;;;;;;21.16;;;;;;;;;;;;;;;;;;;;;;;;;;

Expected Result :

11001;1213;304;;;;;;;111020677.64;;;;;;;;;;;;;;;;;;;;;;;;;;
11002;1213;304;;;;;;;244189.32;;;;;;;;;;;;;;;;;;;;;;;;;;
11003;1215;304;;;;;;;64361.46;;;;;;;;;;;;;;;;;;;;;;;;;;

Any attempts / ideas / thoughts from your side?

First occurrence of what?

First occurance of first field. For example 11001 having many rows. I want to get only first row

What have you tried @arunkumar_mca?

I need to have only 1 and 10 the column so I have cut the record and tried to do sort on key. It dont worked.

 cut -d ";" -f1,10 file| sort -u  -k1,1

I got

11001;102376462.96
11001;105220.65
11001;111020677.64
11001;12348678.19
11001;127834.48
11001;88340.57
11002;220748.79
11002;244189.32
11003;113.89
11003;21.16
11003;64361.46

But I need

11001;102376462.96
11002;220748.79
11003;64361.46

For sort to work you also have to specify the field separator. You can try:

sort -ut\; -k1,1 file

However, this is not standard (POSIX) behavior for sort and cannot be relied upon and some sorts will render very different results.

A better option would be:

awk '!A[$1]++' FS=\; file

---
If you only want to print those two fields, try:

cut -d ";" -f1,10 file | sort -ut\;  -k1,1

or the more reliable:

awk '!A[$1]++{print $1, $10}' FS=\; OFS=\; file
1 Like