Hi,
I have a text file that contains about 15000 lines like the lines below:
IBN         LG   00 0 00 05       304 602 1047      SB            GWLPOT
IBN         LG   00 0 00 11       077 500 2547      SB            GWLPOT
IBN         LG   00 0 00 19       077 574 2000      SB            GWLPOT
IBN         LG   00 0 00 25       303 969 8933      SB            GWLPOT
IBN         LG   00 0 00 44       077 201 6482      SB            GWLPOT
IBN         LG   00 0 00 61       077 572 4001      SB            GWLPOT
IBN         LG   00 0 00 64       077 321 5141      SB            GWLPOT
IBN         LG   00 0 00 68       303 506 8861      SB            GWLPOT
IBN         LG   00 0 00 75       304 832 0986      SB            GWLPOT
IBN         LG   00 0 00 77       077 557 0407      SB            GWLPOT
IBN         LG   00 0 01 11       077 337 7973      SB            GWLPOT

I need to have a perl script runs in windows that will make from the file few files containing in each one no more than 1000 numbers in one line without the number 3 in the begging of the number, at the end they should look like that:
('046021047','0775002547','0775742000','039698933','0772016482','0775724001','0773215141','035068861','048320986','0775570407','0773377973')
In the example you can see 11 numbers in one line.
Thanks in advance.

First you need to read in each line content from the file. An explanation to read & write file in perl can be found here. Then extract the number portion you need (the last 3 number groups in a line) using match functionality (something similar to $string =~ m/regex(capture_group)regex/). Take the matched string and manipulate it the way you want (remove the leading '3'). Then either append to a file or keep it in an array variable. If the array size is 1000, write to file and clear the array. Keep doing until no more to read, write the rest of whatever in the array to file and done.

Hi erezz,
You can achieve what you wanted using perl inbuilt functions. Atleast that is one way to go about it. For example, if your file has a fixed length that the you posted above, one can do like so:

use warnings;
use strict;
use constant ZERO  => 0;
use constant LIMIT => 1000;

my $number_counter = ZERO;
my $key_counter    = ZERO;

my %number_collector;

while (<DATA>) {
    ## use the following functions: unpack, split and join
    ## to get the number needed split on space, then join 
    my $required_number = join '', split / /, unpack( "x34A12", $_ );

    ## modify number that start with a '3', using 
    ## subroutine number_modify
    my $number_to_count = number_modify($required_number);

    if ( $number_counter <= LIMIT ) {  ## check limit here
        $number_counter += scalar split //, $number_to_count;

        ## collect the numbers if limit not reached
        push @{ $number_collector{$key_counter} }, $number_to_count;
    }
    else {
        $number_counter = ZERO;
        $number_collector{ ++$key_counter } = undef;
    }
}

{   ## print out the required
    local $" = "','";

    print "('@{$number_collector{$_}}')" for keys %number_collector;
}

sub number_modify {
    my $number = shift;
    $number = substr( $number, 1 ) if $number =~ m/^3/;
    return $number;
}

__DATA__
IBN         LG   00 0 00 05       304 602 1047      SB            GWLPOT
IBN         LG   00 0 00 11       077 500 2547      SB            GWLPOT
IBN         LG   00 0 00 19       077 574 2000      SB            GWLPOT
IBN         LG   00 0 00 25       303 969 8933      SB            GWLPOT
IBN         LG   00 0 00 44       077 201 6482      SB            GWLPOT
IBN         LG   00 0 00 61       077 572 4001      SB            GWLPOT
IBN         LG   00 0 00 64       077 321 5141      SB            GWLPOT
IBN         LG   00 0 00 68       303 506 8861      SB            GWLPOT
IBN         LG   00 0 00 75       304 832 0986      SB            GWLPOT
IBN         LG   00 0 00 77       077 557 0407      SB            GWLPOT
IBN         LG   00 0 01 11       077 337 7973      SB            GWLPOT

Hope this help.

@2teez, nice explanation :)

After I think about using match, I think there is no reason to match if a sub string is already found. Why not using sub instead...

# rather do this
$number = substr( $number, 1 ) if $number =~ m/^3/;

# how about...
$number =~ s/^3//;

Hi Taywin,

how about...
$number =~ s/^3//;

Why not? It even makes it alot easiler.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.