CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
remove duplicate lindes from file and sort it in specific way

 



tifrah2008
New User

May 23, 2010, 6:31 AM

Post #1 of 2 (2323 views)
remove duplicate lindes from file and sort it in specific way Can't Post

hi,

i have a file which looks like this :

MAP;CTRL;map1
MAP;GFT;map1
MAP;HLRC;map2
MRE-MGW;BROADCAST;cohosted1
MRE-MGW;GFT;cohosted1
OMAP;OMAP;omap1
OMAP;OMAPDB;omap1
OMAP;TRACESDB;omap2
SFE;DBSEI;sfe1;
SFE;DBSEI;sfe1
SFE;DBSEI;sfe1
SFE;GFT;sfe1;
SFE;TRACE_EVENTS;sfe1

i want it to looik like this :

MAP;GFT,CTRL,HLRC,MAP;map1,map2

MRE-MGW;BROADCAST,GFT;cohosted1

OMAP;OMAP,OMAPDB,TRACESDB;omap1,omap2

SFE;DBSEI,GFT,TRACE_EVENTS;sfe1

meaning only 4 lines. the duplicate lines of the first token will be shutter.

and the rest of the values will be in oone line.

how can i do it using perl ?


shawnhcorey
Enthusiast


May 23, 2010, 6:43 AM

Post #2 of 2 (2322 views)
Re: [tifrah2008] remove duplicate lindes from file and sort it in specific way [In reply to] Can't Post

Try:

Code
#!/usr/bin/perl 

use strict;
use warnings;

my %types = ();
my %places = ();

while( <DATA> ){
chomp;
my ( $key, $type, $place ) = split /\;/, $_;
$types{$key}{$type} ++;
$places{$key}{$place} ++;
}

for my $key ( sort keys %types ){
my @types = sort keys %{ $types{$key} };
my @places = sort keys %{ $places{$key} };
print join( ';', $key, join( ',', @types ), join( ',', @places ) ), "\n";
}


__DATA__
MAP;CTRL;map1
MAP;GFT;map1
MAP;HLRC;map2
MRE-MGW;BROADCAST;cohosted1
MRE-MGW;GFT;cohosted1
OMAP;OMAP;omap1
OMAP;OMAPDB;omap1
OMAP;TRACESDB;omap2
SFE;DBSEI;sfe1;
SFE;DBSEI;sfe1
SFE;DBSEI;sfe1
SFE;GFT;sfe1;
SFE;TRACE_EVENTS;sfe1


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives