Connecting Tech Pros Worldwide Forums | Help | Site Map

Extracting data from a file

Newbie
 
Join Date: May 2007
Posts: 5
#1: May 14 '07
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 -----------------------

Expand|Select|Wrap|Line Numbers
  1. Total Number Of Inputs From Rajasthan is: 1675
  2. Total Number Of Subscriber Successfully Provisioned for RAJASTHAN CIRCLE are : 1146
  3. Total Number Of Unsuccessfull Provisioning for RAJASTHAN CIRCLE are : 527
  4. Total Number Of Already Two Provisioned Entries are : 433
  5. Total Number Of Not General Entries are : 1
  6. Total Number Of Anant Entries are : 27
  7. Total Number Of One Number Allowed Entries are : 7
  8. Total Number Of Single Number Mobile Entries are : 23
  9. Total Number Of Number Already Provisioned Entries are : 36
  10. Total Number Of No Subscriber Entries are : 0
  11. Total Number Of Invalid Input Number Entries are : 0
  12. Total Number Of Both Mobile Entries are : 0
  13. Total Number Of Indiaone Entries are : 0
  14. Total Number Of Bundled Sim Entries are : 0
  15. Total Number Of Free SMS Entries are : 0
  16. Total Number Of Invalid LSA Entries are : 0
  17. Total Number Of Preactive Entries are : 0
  18. Total Number Of Student Power Entries are : 0
  19. Total Number Of Not 91 Entries are : 0
  20. Total Number Of More than Two Request Entries are : 0
  21. Total Number Of No Request Entries are : 0
  22. Total Number Of Not 6 Months Entries For New FnF Update Request are : 0
  23.  
  24. Total Number Of Inputs From Punjab is: 718
  25. Total Number Of Subscriber Successfully Provisioned for PUNJAB CIRCLE are : 438
  26. Total Number Of Unsuccessfull Provisioning for PUNJAB CIRCLE are : 280
  27. Total Number Of Already Two Provisioned Entries are : 197
  28. Total Number Of Not General Entries are : 3
  29. Total Number Of Anant Entries are : 14
  30. Total Number Of One Number Allowed Entries are : 3
  31. Total Number Of Single Number Mobile Entries are : 34
  32. Total Number Of Number Already Provisioned Entries are : 12
  33. Total Number Of No Subscriber Entries are : 0
  34. Total Number Of Invalid Input Number Entries are : 0
  35. Total Number Of Both Mobile Entries are : 0
  36. Total Number Of Indiaone Entries are : 0
  37. Total Number Of Bundled Sim Entries are : 17
  38. Total Number Of Free SMS Entries are : 0
  39. Total Number Of Invalid LSA Entries are : 0
  40. Total Number Of Preactive Entries are : 0
  41. Total Number Of Student Power Entries are : 0
  42. Total Number Of Not 91 Entries are : 0
  43. Total Number Of More than Two Request Entries are : 0
  44. Total Number Of No Request Entries are : 0
  45. Total Number Of Not 6 Months Entries For New FnF Update Request are : 0
  46.  
And the output should be like the following .


Expand|Select|Wrap|Line Numbers
  1. 1675 1146 527 433 1 27 7 23 36 0 0 0 0 0 0 0 0 0 0 0 0
  2.  
  3. 718 438 280 197 3 14 3 34 12 0 0 0 0 17 0 0 0 0 0 0 0 0
  4.  

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

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

Expand|Select|Wrap|Line Numbers
  1. open(INPUT, "report.txt");
  2.  
  3. while (chop(my $Line =<INPUT>)) {
  4.  
  5.     if( $Line =~ /Total Number Of Inputs From / ) {
  6.         print "\n" ;
  7.     }
  8.  
  9.     ($String, $Digit) = split /:/, $Line ;
  10.  
  11.     print $Digit ;
  12. }
  13.  
  14.  
miller's Avatar
Moderator
 
Join Date: Oct 2006
Location: San Francisco, CA
Posts: 830
#2: May 14 '07

re: Extracting data from a file


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.

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2.  
  3. open (INPUT, "report.txt") or die "Cant open report.txt: $!";
  4.  
  5. while (my $line = <INPUT>) {
  6.     chomp $line;
  7.  
  8.     if( $line =~ /Total Number Of Inputs From / ) {
  9.         print "\n";
  10.     }
  11.  
  12.     my ($string, $digit) = split /:/, $line;
  13.  
  14.     if ($digit ne '') {
  15.         $digit =~ s/\s+//g; # Removes spaces from around number
  16.         print "$digit ";
  17.     }
  18. }
  19.  
  20. close INPUT;
  21.  
- Miller
Newbie
 
Join Date: Sep 2006
Location: St. Louis, MO
Posts: 8
#3: May 14 '07

re: Extracting data from a file


perl -pe 'chomp;print "\n" if $i++ && m/Total Number Of Inputs/;$_=(split/:/)[1];END {print "\n"}' report.txt
savanm's Avatar
Member
 
Join Date: Oct 2006
Location: chennai
Posts: 86
#4: May 15 '07

re: Extracting data from a file


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);
savanm's Avatar
Member
 
Join Date: Oct 2006
Location: chennai
Posts: 86
#5: May 15 '07

re: Extracting data from a file


$line=~s/([^:]+):([^\n]+)(\n)?/$2/sg;
miller's Avatar
Moderator
 
Join Date: Oct 2006
Location: San Francisco, CA
Posts: 830
#6: May 15 '07

re: Extracting data from a file


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:

Expand|Select|Wrap|Line Numbers
  1. perl -pe 's/.*\b(\d+)\s*$/$1 /' report.txt
  2.  
Output is:

Expand|Select|Wrap|Line Numbers
  1. 1675 1146 527 433 1 27 7 23 36 0 0 0 0 0 0 0 0 0 0 0 0 0 
  2. 718 438 280 197 3 14 3 34 12 0 0 0 0 17 0 0 0 0 0 0 0 0
  3.  
- Miller
fetaelin's Avatar
Newbie
 
Join Date: May 2007
Posts: 17
#7: May 15 '07

re: Extracting data from a file


Quote:

Originally Posted by miller

Expand|Select|Wrap|Line Numbers
  1. perl -pe 's/.*\b(\d+)\s*$/$1 /' report.txt
  2.  


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
Reply