如果其他列值相同,perl代码将连接列的值

时间:2015-01-23 16:10:53

标签: perl

如果其他列value1相同,我想编写perl代码来连接column2的值。我的输入是制表符分隔的,包含3个cloumns。

输入文件:

Date                 Server        application
01/02/2013 00:00     abc123        perl_module_1
01/02/2013 00:00     abc123        oracle_patch_201
03/05/2014 00:00     abc123        Microsoft_patch_71
04/04/2015 00:00     xyz1          oracle_patch_201
02/12/2015 00:00     xyz1          Cygwin_app

输出:

abc123 = ("perl_module_1","oracle_patch_201","Microsoft_patch_71")
xyz1 = ("oracle_patch_201","Cygwin_app")

我的代码如下。你可以看到它不够好

#!usr/bin/perl

use strict;
use warnings;

my $file = 'oneplatformserver.txt';
open my $info, $file or die "could not open $file: $!";
my $application_string="";
my $date_string="";

while (my $line= <$info>) 
{

 chomp $line;
 my @values = split('\t', $line);

    my $application= $values[2];
   # $application =~ s/^\s+|\s+$//g; # This command will trim spaces at the end of the line
    my $Quoteapplication = '"'.$application.'"';
    my $QuoteDate = '"'.$oracledate.'"';

    $application_string = join(',',$application_string,$Quoteapplication);

    $date_string = join(',',$date_string,$QuoteDate);
    print "Date_String is $date_string \n";
    printf("UPA \"TSTCM2:%s.A;1\" /ATTRIBUTES=(application_ID=[ %s ],APPLIED_DATE=[ %s ])", $server,$application_string,$date_string);  

 }
close $info;

# printf("UPB \"QAS:%s.A;1\" /ATTRIBUTES=(application_ID=[ %s ],APPLIED_DATE=[ %s ])", $server,$applicationstring,$datestring);

2 个答案:

答案 0 :(得分:2)

当你在谈论关键值对时,答案是“使用哈希”#39;

my %applications_on;

while ( <$info> ) {
   chomp;
   my ( $date, $time, $server, $application ) = split; 
   push ( @{ $applications_on{$server} }, $application );
}

foreach my $server ( keys %applications_on ) {
    print "($server) = ".join ( ",", @{ $applications_on{$server} } ); 
}

无论如何都是这样的。

答案 1 :(得分:1)

use strict; 
use warnings; 
use 5.016;
use Data::Dumper;

my $fname = 'data.txt';

open my $INFILE, '<', $fname
    or die "Couldn't open $fname: $!";

my $header = <$INFILE>;
say "0123456789" x 7;
say $header;

my %apps_for;

while (my $line = <$INFILE>) {
    my ($server, $app) = unpack '@21 A14 A*', $line;  #see explanation below

    my $trailing_whitespace = qr{\s* \z}xms;
    $server =~ s/$trailing_whitespace//;
    $app =~ s/$trailing_whitespace//;

    push @{$apps_for{$server}}, $app;
}

close $INFILE;
say Dumper(\%apps_for);

for my $server (keys %apps_for) {
    say "$server:";
    say "  $_" for @{$apps_for{$server}};
}


--output:--
0123456789012345678901234567890123456789012345678901234567890123456789
Date                 Server        application

$VAR1 = {
          'xyz1' => [
                      'oracle_patch_201',
                      'Cygwin_app'
                    ],
          'abc123' => [
                        'perl_module_1',
                        'oracle_patch_201',
                        'Microsoft_patch_71'
                      ]
        };

xyz1:
  oracle_patch_201
  Cygwin_app
abc123:
  perl_module_1
  oracle_patch_201
  Microsoft_patch_71

...

@21 A14 A*   @21 -> move to position 21
             A14 -> extract 14 characters(A)
             A*  -> extract the remaining(*) characters(A) 

如果您真的想要显示的确切输出:

for my $server (keys %apps_for) {
    local $" = ', ';
    say "$server = (@{$apps_for{$server}})";
}

--output:--
xyz1 = (oracle_patch_201, Cygwin_app)
abc123 = (perl_module_1, oracle_patch_201, Microsoft_patch_71)
  

$&#34;

     

将数组或数组切片插入到双引号中   字符串或类似的上下文,如/.../,其元素是分开的   通过这个值。默认是一个空格   http://perldoc.perl.org/perlvar.html