-1

I would like to find the first unique occurrence of a match within a file. In the example data below, I would like to find the first occurrence of each Chunk

Chunk 1
some text
second line of random text
Chunk 2
some text
second line of random text
Chunk 3
some text
second line of random text
Chunk 1
some text
second line of random text
Chunk 3
some text
second line of random text
Chunk 2
some text
second line of random text

Chunks[1-8], will return all the instances of the matches, I just want the first time each unique Chunk number is listed. Regex will work, so I can use in Notepad++ and eventually incorporate into a python script. I'd also like to return the unique match, along with the "second line of random text" after each unique match

So what I would like to see is

Chunk 1 (first occurrence)
second line of random text
Chunk 2 (first occurrence)
second line of random text<br>
Chunk 3 (first occurrence)
second line of random text
  • 2
    What's "Notepad++"? Are you sure you're on the right stackexchange site? – Stéphane Chazelas Jul 1 '16 at 20:57
2

With GNU awk:

gawk -v 'RS=Chunk [0-9]+\n' -v ORS= '
  {$0=lastRT $0}
  NR>1 && !seen[$0]++
  {lastRT = RT}'
0

With perl. Not particularly readable, awk approach far better.

perl -ne 'if(/^Chunk [0-9]+$/&&!exists($seen{$_})){$seen{$_}++;chomp;$a=$_;$_=<>;$_=<>;print "$a $_"}'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.