vis1982 has asked for the wisdom of the Perl Monks concerning the following question:

Question is u have a file A

ACEEWSMKIIWSDJDWKDKEKSSAQWE

and want to extract lines with option 5-10 with output like

SMKII

The code is
#!/usr/bin/perl use strict; use warnings; if ( $#ARGV < 2 ) { print "Usage: extract.pl firstLine lastLine filename\n"; exit 1; } my $start = $ARGV[0]; my $stop = $ARGV[1]; my $file = $ARGV[2]; open (FILE,$file) || die "Can't open file \"$file\".\n"; my $count=0; while () { $count++; if ( $count >= $start && $count <= $stop ) { print ; } } close(FILE);
But Gave error.What can be possible error?

Replies are listed 'Best First'.
Re: print and extract the line
by Corion (Patriarch) on Jul 07, 2010 at 13:04 UTC

    What error did it "give"?

    What are you actually trying to do?

    Are you aware that you are not actually reading from your file?

    Maybe you want to use App::Ack?

    Have you seen the $. variable? See perlvar.

      Sorry, i didn't notice your reply on $. in this thread.
Re: print and extract the line
by Neighbour (Friar) on Jul 07, 2010 at 13:06 UTC
      The read will work, but that won't solve OP's (ill-specced) problem.
Re: print and extract the line
by ww (Archbishop) on Jul 07, 2010 at 14:08 UTC
    vis1982, do you mean to extract lines (as your variable names suggest) or characters from a line within a file within the range specified on the commandline?
        From the reply ABOVE, posted as this was prepared, I think the answer is 'yes.' Do others read it thus?

    If characters within a line, you need not only to actually read the file (as Corion mentioned); you need to process the current line... and incrementing $count won't do that for you.

    Update: adding code (and subsequently, removing some debug matter)

    #!/usr/bin/perl use strict; use warnings; # 848449 =head OP says: Question is u have a file A ACEEWSMKIIWSDJDWKDKEKSSAQWE and want to extract lines with option 5-10 with output like SMKII ## ww reads this as: I have a file with lines like ACEEWSMKIIWSDJDWKDKEKSSAQWE 012345678901234567890123456789 0 1 2 and want to extract characters at positions, 5 .. 9, where the character position is 0-based. =cut if ( $#ARGV < 2 ) { print "Usage: extract.pl firstLine lastLine filename\n"; exit 1; } my $start = $ARGV[0]; my $stop = $ARGV[1]; my $file = $ARGV[2]; my ($line_elements, @line_elements); open (FILE, '<', $file) or die "Can't open file $file $!"; while (<FILE>) { my $line = $_; my @line_elements = split (//,$line); my (@wanted, $wanted); for ($start .. ($stop-1) ) { push @wanted, $_; } for $wanted(@wanted) { for ($line_elements[$wanted]) { print "$line_elements[$wanted]"; } } print "\n\tNext line in file: \n"; }

    where content of the input file, 848449.3line.txt, exceeds OP's original requirement, and is:

    ACEEWSMKIIWSDJDWKDKEKSSAQWE abcdefghijklxxxxmnop 12345---678xxxx

    where CLI and output is

    >perl 848449.pl 5 10 848449.3line.txt SMKII Next line in file: fghij Next line in file: ---67 Next line in file: >
    hth.
      I want to extarct characters from a line within the specific range Tht is extract form a line...

        I can't quite make head or tails of your posts, but I'm bored anyhow, so here's my take:

        #! /usr/bin/perl -w use strict; die "Not enough arguments" if $#ARGV < 2; my ($start, $end, $file) = @ARGV; open FILE, $file or die "Couldn't open file"; while (<FILE>) { my @chars = split //; print @chars[$start..$end], "\n" if @chars >= $end; }

        It's quite a derail, but if someone could point me towards a node with examples or some sort of documentation of autodie, I'd be grateful (my Super Search-fu isn't strong).

Re: print and extract the line
by Tux (Canon) on Jul 07, 2010 at 13:27 UTC

    Looking at the comment inside the code, all you want is a one-liner ...

    $ perl -wne'5 .. 10 and print' file

    Or in your script, more verbose

    #!/usr/bin/perl use strict; use warnings; use autodie; sub usage { my $err = shift and select STDERR; print "usage: extract.pl firstline lastline filename\n"; exit $err; } # usage use Getopt::Long qw(:config bundling nopermute); GetOptions ( "help|?" => sub { usage (0); }, ) or usage (1); my ($start, $stop, $file) = @ARGV; -s $file && $start =~ m/^[0-9]+$/ && $stop =~ m/^[0-9]+$/ && $start <= + $stop or usage (1); open my $fh, "<", $file; while (<$fh>) { $. >= $start && $. <= $stop and print "$.: $_"; } close $fh;

    Doing this on that code:

    $ perl extract.pl 5 8 extract.pl 5: use autodie; 6: 7: sub usage 8: { $

    Yeah, way to complex

    $ perl -wne'5..8 and print' extract.pl use autodie; sub usage { $

    Enjoy, Have FUN! H.Merijn
      Sorry It does not seem the output that i want it is like i have posted the wrong question with different meaning

      As I want is

      have a file A in one line

      ASDSSDSDDDAAAAEDCDALSOSSKDQWSDSD

      i want to extract where say from 4 to 8

      and the output print is

      SSDSD
        To extract a subset of characters from a string, use substr(). See substr.

        #!usr/bin/perl -w use strict; my $x = 'ASDSSDSDDDAAAAEDCDALSOSSKDQWSDSD'; print substr($x,4-1,8-4+1); #strings start at index 0 and not index 1. #prints SSDSD
Re: print and extract the line
by johngg (Canon) on Jul 07, 2010 at 16:12 UTC

    From the lines and expected outputs you have shown it looks like you want a character range from within a string. You can use substr for this. Note that Perl uses zero-based offsets into strings.

    $ perl -E ' > $line = q{abcdefghijklmno}; > say $line; > say substr $line, 0, 3; > say substr $line, 5, 6; > say substr $line, 13;' abcdefghijklmno abc fghijk no $

    I hope this is helpful.

    Cheers,

    JohnGG

Re: print and extract the line
by Fletch (Bishop) on Jul 07, 2010 at 13:14 UTC
Re: print and extract the line
by suhailck (Friar) on Jul 07, 2010 at 13:17 UTC
    I think you can also use "$." in place of "$count" in your code.
    "man perlvar"

    $. Current line number for the last filehandle accessed.