in reply to print and extract the line

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.

Replies are listed 'Best First'.
Re^2: print and extract the line
by vis1982 (Acolyte) on Jul 07, 2010 at 14:51 UTC
    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).

        + + for effort & brevity

        Note, however, that this code prints six-character strings, whereas OP's example output (in the narrative) looks for only five.

        As for autodie and your search fu, Super Search doesn't require a whole lot of fu;
        here's the output where the only input was "autodie": ?node_id=3989;BIT=autodie