Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: make a search pattern and remove duplicate from the file

by jimpudar (Pilgrim)
on May 11, 2018 at 04:24 UTC ( [id://1214364]=note: print w/replies, xml ) Need Help??


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

You should use HTML tags to format your post. It's very difficult to tell what you are asking the way you posted it!

I'm assuming you meant this:

Hi , I have a file which have input as like below pet="cat"|hate="rat"|like="dog" hate="rat"|like="dog"|pet="cat" hate="rat"|like="horse"|pet="cat" pet="cow"|hate="rat"|like="dog" hate="rat"|like="dog"|pet="cow" And output im looking is pet="cat"|hate="rat"|like="dog" pet="cow"|hate="rat"|like="dog" Any first instance where I find unique value for pet and hate I wanted + to print. Thanks

You should have tried your hand at writing a solution and posted the code, but since I'm feeling generous here is a solution which you can pipe your input file to:

#! /usr/bin/env perl use strict; use warnings; my %pet_hate; while (<>) { my ($pet) = /pet="(\w+)"/ or next; my ($hate) = /hate="(\w+)"/ or next; my ($like) = /like="(\w+)"/ or next; $pet_hate{"$pet:$hate"} //= { pet => $pet, hate => $hate, like => $like, }; } foreach (values %pet_hate) { printf(qq{pet="%s"|hate="%s"|like="%s"\n}, $_->{pet}, $_->{hate}, $_->{like}); }

Best,

Jim

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1214364]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-25 22:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found