Extracting data from a file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Amma
    New Member
    • May 2007
    • 5

    Extracting data from a file

    Hello Every one ,

    Pls help me to extracting number from a text file since I am new to perl programming .

    I have a file and need to extract the number after semicolon in that

    -----------------Contents of the file -----------------------

    Code:
    Total Number Of Inputs From Rajasthan is: 1675
    Total Number Of Subscriber Successfully Provisioned for RAJASTHAN CIRCLE are : 1146
    Total Number Of Unsuccessfull Provisioning for RAJASTHAN CIRCLE are : 527
    Total Number Of Already Two Provisioned Entries are : 433
    Total Number Of Not General Entries are : 1
    Total Number Of Anant Entries are : 27
    Total Number Of One Number Allowed Entries are : 7
    Total Number Of Single Number Mobile Entries are : 23
    Total Number Of Number Already Provisioned Entries are : 36
    Total Number Of No Subscriber Entries are : 0
    Total Number Of Invalid Input Number Entries are : 0
    Total Number Of Both Mobile Entries are : 0
    Total Number Of Indiaone Entries are : 0
    Total Number Of Bundled Sim Entries are : 0
    Total Number Of Free SMS Entries are : 0
    Total Number Of Invalid LSA Entries are : 0
    Total Number Of Preactive Entries are : 0
    Total Number Of Student Power Entries are : 0
    Total Number Of Not 91 Entries are : 0
    Total Number Of More than Two Request Entries are : 0
    Total Number Of No Request Entries are : 0
    Total Number Of Not 6 Months Entries For New FnF Update Request are : 0
    
    Total Number Of Inputs From Punjab is: 718
    Total Number Of Subscriber Successfully Provisioned for PUNJAB CIRCLE are : 438
    Total Number Of Unsuccessfull Provisioning for PUNJAB CIRCLE are : 280
    Total Number Of Already Two Provisioned Entries are : 197
    Total Number Of Not General Entries are : 3
    Total Number Of Anant Entries are : 14
    Total Number Of One Number Allowed Entries are : 3
    Total Number Of Single Number Mobile Entries are : 34
    Total Number Of Number Already Provisioned Entries are : 12
    Total Number Of No Subscriber Entries are : 0
    Total Number Of Invalid Input Number Entries are : 0
    Total Number Of Both Mobile Entries are : 0
    Total Number Of Indiaone Entries are : 0
    Total Number Of Bundled Sim Entries are : 17
    Total Number Of Free SMS Entries are : 0
    Total Number Of Invalid LSA Entries are : 0
    Total Number Of Preactive Entries are : 0
    Total Number Of Student Power Entries are : 0
    Total Number Of Not 91 Entries are : 0
    Total Number Of More than Two Request Entries are : 0
    Total Number Of No Request Entries are : 0
    Total Number Of Not 6 Months Entries For New FnF Update Request are : 0
    And the output should be like the following .


    Code:
    1675 1146 527 433 1 27 7 23 36 0 0 0 0 0 0 0 0 0 0 0 0
    
    718 438 280 197 3 14 3 34 12 0 0 0 0 17 0 0 0 0 0 0 0 0

    I have written the following code but it's not working on the above file.

    -----------------------------Code -----------------------------------

    [CODE=perl]
    open(INPUT, "report.txt ");

    while (chop(my $Line =<INPUT>)) {

    if( $Line =~ /Total Number Of Inputs From / ) {
    print "\n" ;
    }

    ($String, $Digit) = split /:/, $Line ;

    print $Digit ;
    }

    [/CODE]
  • miller
    Recognized Expert Top Contributor
    • Oct 2006
    • 1086

    #2
    Hello Amma,

    You are almost there. You simply needed to check to see if the $digit value existed to take into account blank lines, and then simply remove spaces from around the $digit value.

    [CODE=perl]
    use strict;

    open (INPUT, "report.txt ") or die "Cant open report.txt: $!";

    while (my $line = <INPUT>) {
    chomp $line;

    if( $line =~ /Total Number Of Inputs From / ) {
    print "\n";
    }

    my ($string, $digit) = split /:/, $line;

    if ($digit ne '') {
    $digit =~ s/\s+//g; # Removes spaces from around number
    print "$digit ";
    }
    }

    close INPUT;
    [/CODE]

    - Miller

    Comment

    • ValHolla
      New Member
      • Sep 2006
      • 8

      #3
      perl -pe 'chomp;print "\n" if $i++ && m/Total Number Of Inputs/;$_=(split/:/)[1];END {print "\n"}' report.txt

      Comment

      • savanm
        New Member
        • Oct 2006
        • 85

        #4
        Hi...
        use strict;
        open (INPUT, "report.txt ") or die "Cannot open report.txt: $!";
        while (my $line = <INPUT>) {
        $line=~s/([^:]+):([^\n]+)(\n)?/$2/sg;
        print $line;
        }
        close(INPUT);

        Comment

        • savanm
          New Member
          • Oct 2006
          • 85

          #5
          $line=~s/([^:]+):([^\n]+)(\n)?/$2/sg;

          Comment

          • miller
            Recognized Expert Top Contributor
            • Oct 2006
            • 1086

            #6
            Gentlemen,

            You're very amusing. However, the best way to help someone is to use their own code to show them how to finish a project.

            One-liners, while nice to know, are rarely the best solution for a problem. But if you really want to create one:

            Code:
            perl -pe 's/.*\b(\d+)\s*$/$1 /' report.txt
            Output is:

            Code:
            1675 1146 527 433 1 27 7 23 36 0 0 0 0 0 0 0 0 0 0 0 0 0 
            718 438 280 197 3 14 3 34 12 0 0 0 0 17 0 0 0 0 0 0 0 0
            - Miller

            Comment

            • fetaelin
              New Member
              • May 2007
              • 16

              #7
              Originally posted by miller
              Code:
              perl -pe 's/.*\b(\d+)\s*$/$1 /' report.txt

              hi..
              that was imporesting :-)
              can you please describe for me what
              perl -pe 's/.*\b(\d+)\s*$/$1 /' report.txt
              does ? part by part.
              thanx

              Comment

              Working...