I have multiple files like data.txt:
@NS500278:42:HC7M3AFXX:1:11101:16723:1045 1:N:0:AACGTGAT AGATCNGAAGAGCACACGTCTGAACTCCAGTCACAACGTGATATCTCGTATGCCGTCTTCTGCTTGAAAA +AAAAAAGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG +GGGGGGGGGGGG + AAAAA#EEEEEEEEEEEEEEEE6EEEEEAEEEAE/AEEEEEEEAE<EEEEA<A/AE<EE/EEAEEAEEAE +EEEA///EEEEEEEEEAEEEEEEEEEEEEEEEEEEEE/EEEAEEEAEEEEEEEEEAEAEEEEEEEEEEE +EAEEEEEAEEAA @NS500278:42:HC7M3AFXX:1:11101:20279:1046 1:N:0:AACGTGAT TACAGNGAGCAAACTGAAATGAAAAAGAAATTAATCAGCGGACTGTTTCTGATGTTATGGATGGCGCTGT +TAATCGCAGCAATGGTGTATCCGCAGGGGATTTTTCCGGTACTGGCAGCGTCCGGCGTTTGGGTAGAGA +TCGGAAGAGCAC + AAAAA#EEEEEEEEAEEEEEEEEEEEEEEEEEEEEAEEEEEEEE/EEEAE6AE<EAEEAEAAEEAEEEEE +EEAE/EEAEEAEEE6EEEEEAE6A/E<EEEEEEEEAE<EEEEEA/AEEAAEEEEEE//AEE/<<<EEAE +<66/</AE<<A6 @NS500278:42:HC7M3AFXX:1:11101:18609:1046 1:N:0:AACGTGAT TACAGNGAGCAAACTGAAATGAAAAAGAAATTAATCAGCGGACTGTTTCTGATGTTATGGATGGCGCTGT +TAATCGCAGCAATGGTGTATCCGCAGGGGATTTTTCCGGTACTGGCAGCGTCCGGCGTTTGGGTAGAGA +TCGGAAGAGCAC + AAAAA#EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEAEEEEEEEEAEEEAEEEEAEEAEEEE +AEEEA//EEAEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE +EEAAAEAEEEEA
I want to print the whole paragraph (four lines) and the count of second line in the file. The output for above file should be:
@NS500278:42:HC7M3AFXX:1:11101:16723:1045 1:N:0:AACGTGAT AGATCNGAAGAGCACACGTCTGAACTCCAGTCACAACGTGATATCTCGTATGCCGTCTTCTGCTTGAAAA +AAAAAAGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG +GGGGGGGGGGGG + AAAAA#EEEEEEEEEEEEEEEE6EEEEEAEEEAE/AEEEEEEEAE<EEEEA<A/AE<EE/EEAEEAEEAE +EEEA///EEEEEEEEEAEEEEEEEEEEEEEEEEEEEE/EEEAEEEAEEEEEEEEEAEAEEEEEEEEEEE +EAEEEEEAEEAA 1 @NS500278:42:HC7M3AFXX:1:11101:20279:1046 1:N:0:AACGTGAT TACAGNGAGCAAACTGAAATGAAAAAGAAATTAATCAGCGGACTGTTTCTGATGTTATGGATGGCGCTGT +TAATCGCAGCAATGGTGTATCCGCAGGGGATTTTTCCGGTACTGGCAGCGTCCGGCGTTTGGGTAGAGA +TCGGAAGAGCAC + AAAAA#EEEEEEEEAEEEEEEEEEEEEEEEEEEEEAEEEEEEEE/EEEAE6AE<EAEEAEAAEEAEEEEE +EEAE/EEAEEAEEE6EEEEEAE6A/E<EEEEEEEEAE<EEEEEA/AEEAAEEEEEE//AEE/<<<EEAE +<66/</AE<<A6 2
The code which I have written is:
use strict; use warnings; my @files=('data.txt'); for my $input_file (@files) { my $output_file = $input_file.".out"; process_file($input_file, $output_file); } sub process_file { my($input_file, $output_file) = @_; my %count; my $file = shift or die "Usage: $0 FILE\n"; open my $fh, '<', $file or die "Could not open '$file' $!"; open my $fa, '>', $output_file; $/=""; while (my $line = <$fh>) { foreach my $str ($line) { chomp $line; $count{$str}++; } } foreach my $str (sort keys %count) { printf $fa "%-s %s", $str."\t", $count{$str}; print $fa ":".$input_file."\n"; } }
This gives the count of whole paragraph instead of the second line. It matches the whole paragraph but I want to print the whole paragraph and count of only the second line.

In reply to count the occurrence of second line of a paragraph in a file by umaykulsum

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.