473,413 Members | 1,767 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Extracting data from a file

5
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.  
May 14 '07 #1
6 2597
miller
1,089 Expert 1GB
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
May 14 '07 #2
perl -pe 'chomp;print "\n" if $i++ && m/Total Number Of Inputs/;$_=(split/:/)[1];END {print "\n"}' report.txt
May 14 '07 #3
savanm
85
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);
May 15 '07 #4
savanm
85
$line=~s/([^:]+):([^\n]+)(\n)?/$2/sg;
May 15 '07 #5
miller
1,089 Expert 1GB
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
May 15 '07 #6
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
May 15 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Steve | last post by:
Hi, I have a very long string, someting like: DISPLAY=localhost:0.0,FORT_BUFFERED=true, F_ERROPT1=271\,271\,2\,1\,2\,2\,2\,2,G03BASIS=/opt/g03b05/g03/basis,...
2
by: Avi | last post by:
hi, Can anyone tell me what the problem is and how to solve it The following piece of code resides on an asp page on the server and is used to download files from the server to the machine...
0
by: Nadav | last post by:
Hi, Introduction: *************************** I am using the MSI API to extract MSI embedded files, I do this by iterating through all of the records in the ‘_Streams’ table and dumping...
13
by: Randy | last post by:
Is there any way to do this? I've tried tellg() followed by seekg(), inserting the stream buffer to an ostringstream (ala os << is.rdbuf()), read(), and having no luck. The problem is, all of...
0
by: Sunil Basu | last post by:
Hi, I have a interesting thing to know and discuss with you. I am extracting data from an Excel file in a Delphi DbGrid through SQL. I want to create a criteria on a specific cell value of the...
0
by: sgsiaokia | last post by:
I need help in extracting data from another source file using VBA. I have problems copying the extracted data and format into the required data format. And also, how do i delete the row that is not...
6
by: rlntemp-gng | last post by:
I need to extract information from some Excel files but am stuck with part of it: As an example, I have the following Excel File that has 3 tabbed sheets: FileName: ...
6
by: Werner | last post by:
Hi, I try to read (and extract) some "self extracting" zipefiles on a Windows system. The standard module zipefile seems not to be able to handle this. False Is there a wrapper or has...
4
by: poolboi | last post by:
hi guys i've having some problem extracting data from a text file example if i got a text file with infos like: Date 2008-05-01 Time 22-10 Date 2008-05-01 Time 21-00 Date 2008-05-02 Time...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.