in reply to Re: make a search pattern and remove duplicate from the file
in thread make a search pattern and remove duplicate from the file

I think what he is asking for is to print out only the first instance of any line which has a unique value of the pet, hate tuple.

Any other lines should be ignored.

Since the structure you are building does not keep track of which line came first, looping over it will never bring you to the answer he is looking for.

If you really wanted to do this with a one liner, (and I'm definitely not saying you should), I would do this, which will print exactly the result he asked for:

perl -wlF'\|' -e 'for (@F) { /(\w+)="(\w+)"/; $r{$1} = $2 } $x="$r{pet}:$r{hate}"; $s{$x} ? next : ++$s{$x} && print' <input pet="cat"|hate="rat"|like="dog" pet="cow"|hate="rat"|like="dog"

Best,

Jim

Replies are listed 'Best First'.
Re^3: make a search pattern and remove duplicate from the file
by NetWallah (Canon) on May 11, 2018 at 20:15 UTC
    Your comment propagates the OP's usage of the word "unique" - and that causes confusion because your implementation reports the "first" usage of the tuple, which may not necessarily be a "unique" occurrence.

    In order to find "unique", you will need to do that after all records have been ingested, and post-process, as my code does.

    Anyway - this only illustrates non-specific specifications - and these nits are not worth picking.

    Cheers.

                    Memory fault   --   brain fried

      Very good point, I didn't really think about the meaning of "unique" but just tried to make sense of the desired output.

      Best,

      Jim