in reply to Re^5: compare lines within a file
in thread compare lines within a file

The line

    if ($row->[0] =~ m{\QHWUSI-EAS95L_0025_FC:3:1:5232:1082#0//E}) {

should have read

    if ($row->[0] =~ m{\QHWUSI-EAS95L_0025_FC:3:1:5232:1082#0/\E}) {

There is a typo in the original code with an incorrect slash before the trailing E. The escaped pair \Q and \E tells Perl (when interpolating) to escape all characters that have special meaning in a regular expression - see Quote and Quote like Operators in perlop. The typo was of course mine, and I have corrected the original post accordingly. With that change, I get the output: 1449586    1449577. Obviously, you should be modifying that matching condition to fit your requirements.

Replies are listed 'Best First'.
Re^7: compare lines within a file
by garyboyd (Acolyte) on Mar 15, 2011 at 10:19 UTC

    Thanks kennethk, this works, but I cannot figure out how to incorporate this into a program that can take a list and then check each line of the list to see if subsequent entries compare and if they do to then print out.

    It is possible to do this if the name is specified in the code

    if ($row->[0] =~ m{\QHWUSI-EAS95L_0025_FC:3:1:5232:1082#0/\E}) {

    but how to do this if you do not know what each line is??

    I tried using the following regex, which will match each line:

             if ($header2 =~  m{HWUSI-EAS95L_0025_FC:3:1:\d*:\d*#0/}) {

    but not sure how to take it from there.....

      My general approach in this type of situation is to build a hash, or more specifically a hash of lists. I am not familiar with your particular input, and figuring out the patterns and how to parse them is usually where most of the effort in writing a script of this type falls. Taking the input from your original post:

      HWUSI-EAS95L_0025_FC:3:1:5232:1082#0/1 - 1449586 1449619 HWUSI-EAS95L_0025_FC:3:1:5232:1082#0/2 - 1449544 1449577 HWUSI-EAS95L_0025_FC:3:1:6417:1078#0/1 - 4744083 4744113 HWUSI-EAS95L_0025_FC:3:1:6539:1083#0/1 - 4867122 4867157 HWUSI-EAS95L_0025_FC:3:1:6539:1083#0/2 - 4866942 4866977 HWUSI-EAS95L_0025_FC:3:1:10260:1083#0/1 + 1930232 1930266 HWUSI-EAS95L_0025_FC:3:1:10260:1083#0/2 + 1930354 1930389

      And fitting the most general pattern that seems to match, I would use code like

      #!/usr/bin/perl use strict; use warnings; use Text::CSV; my @result; my $csv = Text::CSV->new ( { sep_char => "\t" } ) # should set binary + attribute. or die "Cannot use CSV: ".Text::CSV->error_diag (); my %result; while ( my $row = $csv->getline( *DATA ) ) { my ($key, $index) = $row->[0] =~ m{^(.+)/([12])$} or die "Line did not match pattern: @$row"; if ($index == 1) { $result{$key}[0] = $row->[2]; } elsif ($index == 2) { $result{$key}[1] = $row->[2]; } else { die "Index was not 1 or 2: @$row" } } $csv->eof or $csv->error_diag(); # Output results: for my $key (keys %result) { next unless $result{$key}[1]; print "$key:\t$result{$key}[0]\t$result{$key}[1]\n"; } __DATA__ HWUSI-EAS95L_0025_FC:3:1:5232:1082#0/1 - 1449586 1449619 HWUSI-EAS95L_0025_FC:3:1:5232:1082#0/2 - 1449544 1449577 HWUSI-EAS95L_0025_FC:3:1:6417:1078#0/1 - 4744083 4744113 HWUSI-EAS95L_0025_FC:3:1:6539:1083#0/1 - 4867122 4867157 HWUSI-EAS95L_0025_FC:3:1:6539:1083#0/2 - 4866942 4866977 HWUSI-EAS95L_0025_FC:3:1:10260:1083#0/1 + 1930232 1930266 HWUSI-EAS95L_0025_FC:3:1:10260:1083#0/2 + 1930354 1930389
      to get the output

      HWUSI-EAS95L_0025_FC:3:1:6539:1083#0: 4867122 4866942 HWUSI-EAS95L_0025_FC:3:1:5232:1082#0: 1449586 1449544 HWUSI-EAS95L_0025_FC:3:1:10260:1083#0: 1930232 1930354

      Note that because of how tabs get mangled on this site, you'll need to click the download link in order to get the proper formatting in your clipboard.

        brilliant! thank you so much for your help kennethk