On Sun, Mar 11, 2012 at 11:15 PM, Ron Bergin <r...@i.frys.com> wrote: > lina wrote: >> On Sun, Mar 11, 2012 at 10:45 PM, lina <lina.lastn...@gmail.com> wrote: >>> A >>> 7.803481E-01 8.228973E-01 7.515242E-01 2 1833 >>> -5.5000000000 308.3889771284 5 0 7 1.7084151661 >>> 1.6790503987 2.75458 >>> 53558 >>> 7.866901E-01 8.410519E-01 9.981456E-01 2 14485 >>> -5.5000000000 269.6201271260 39 4 7 -2.5561279716 >>> -3.5975355928 1.5117 >>> 155069 >>> C >>> 7.735338E-01 9.981671E-01 7.735798E-01 2 11514 >>> -5.5000000000 289.1918534266 31 1 7 -5.6311359613 >>> -0.0502358314 0.0768 >>> 146957 >>> 5.907322E-02 6.045568E-02 3.388628E-02 1 28 >>> -6.5000000000 336.0228260493 1 2 7 0.8177802191 >>> 3.9634621584 -3.0314 >>> 370501 >>> A >>> 2.764127E-02 3.230161E-02 1.633790E-02 1 51 >>> -6.5000000000 319.7604886848 1 3 7 0.7583797888 >>> 3.5176580829 -1.87872 >>> 93439 >>> 5.960780E-02 2.111333E-02 1.066835E-01 1 62 >>> -6.5000000000 297.7363059936 1 1 7 2.2257828331 >>> 3.7887567121 -3.4478 >>> 600377 >>> >>> >>> I am so troubled with extract the lines after A but not the lines >>> under C out, so the final result is >>> >>> 7.803481E-01 8.228973E-01 7.515242E-01 2 1833 >>> -5.5000000000 308.3889771284 5 0 7 1.7084151661 >>> 1.6790503987 2.75458 >>> 53558 >>> 7.866901E-01 8.410519E-01 9.981456E-01 2 14485 >>> -5.5000000000 269.6201271260 39 4 7 -2.5561279716 >>> -3.5975355928 1.5117 >>> 1550692.764127E-02 3.230161E-02 1.633790E-02 1 51 >>> -6.5000000000 319.7604886848 1 3 7 0.7583797888 >>> 3.5176580829 -1.87872 >>> 93439 >>> 5.960780E-02 2.111333E-02 1.066835E-01 1 62 >>> -6.5000000000 297.7363059936 1 1 7 2.2257828331 >>> 3.7887567121 -3.4478 >>> 600377 >>> >>> >>> Thanks for you suggestions, >> >> What I have come up so far : >> >> #!/usr/bin/env perl >> >> use strict; >> use warnings; >> >> my $filename = "try.txt"; >> >> open my $fh, '<', $filename or die "Couldn't read $filename"; > > You should include the reason it failed in the die statement. > > open my $fh, '<', $filename or die "Couldn't read $filename <$!>"; >> >> >> >> while (my $line = <$fh>){ >> if ($line =~ /^A$/){ > > Use the range operator to delimit the section you want to extract. > You can read about in 'perldoc perlop' > > if ($line =~ /^A$/ .. $line =~ /^C$/) { > >> ## Here I don't know how to proceed further >> print $line; > > You'll want to skip over the 'A' and 'C' lines before printing the line. > I'll leave that to you. Thank you Ron, I never known how amazing $line =~ /A$/ .. $line =~ /C$/ can do. I don't know how to skip the 'A' and 'C' lines, the only way I could think of using grep -v in the output file. check the "next", but not so sure how to do this,
#!/usr/bin/env perl use strict; use warnings; my $filename = $ARGV[0]; my $outfile = $ARGV[1]; open my $fh, '<', $filename or die "Couldn't open '$filename' for reading: $!"; open my $OUT, '>>', $outfile or die "Couldn't open '$outfile' for writing: $!"; while (my $line = <$fh>){ if ($line =~ /C$/ .. $line =~ /Si$/){ print $OUT $line; } #if ($line =~ /^C/..$line =~ /^A$/){ # print $line; #} } $ perl calculate_COM.pl com.txt A.txt $ more com.txt Si 5 3 1 0 0 0 0 1 0 0 0 0 0 5 5 1 0 0 0 0 5 0 0 0 0 0 3 6 1 0 0 0 0 6 0 0 0 0 0 C 8 3 1 0 0 0 0 1 0 0 0 0 0 3 5 1 0 0 0 0 4 0 0 0 0 0 1 6 1 0 0 0 0 6 0 0 0 0 0 C 1 3 1 0 0 0 0 2 0 0 0 0 0 5 5 1 0 0 0 0 5 0 0 0 0 0 3 6 1 0 0 0 0 6 0 0 0 0 0 Si 1 3 1 0 0 0 0 1 0 0 0 0 0 2 5 1 0 0 0 0 5 0 0 0 0 0 3 6 1 0 0 0 0 6 0 0 0 0 0 I changed the examples to make is simple and readable. Thanks with best regards, > >> } >> } >> >> Thanks >> >> -- >> > > Ron Bergin > -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/