473,321 Members | 1,748 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,321 software developers and data experts.

Contents between two words in a FILE

Hi all,

I need to extact the contents between two keywords(START and STOP in a file .

I cont get the logic how to take the contents...

Can anyone help me out!!!

Expand|Select|Wrap|Line Numbers
  1. $dirname="D:\\Extra\\Perl\\Pat";
  2. open(FH3, ">filelist.txt") || die "Can't create the report file";
  3. open(FH4, ">report.txt") || die "Can't create the report file";
  4.  
  5. unless (opendir (DIR,"$dirname")){
  6.     print "Can't open the directory $dirname \n";
  7.     exit;
  8. }
  9.  
  10. while ($file = readdir(DIR)) {
  11.     print FH3 "$file\n";
  12. }
  13. close(FH3);
  14.  
  15. #***************Actual code*********************
  16. $write=0;
  17. open (FH, qq*<filelist.txt*) || die "Can't find the filelist.txt";
  18. while(chomp( $mfile=<FH>)) {
  19.     if (-e $mfile && $mfile=~/txt$/) {
  20.         open (FH1,"<$mfile");
  21.         while(<FH1>) {
  22.             if ($text =~ /START/)
  23.             }
  24.         }
  25.     }
  26. }
  27.  
Jul 23 '07 #1
7 1574
Thanks I found the solution to the Problem
Jul 23 '07 #2
numberwhun
3,509 Expert Mod 2GB
And what were your findings?

*Hint: There are lots of people who visit forums like this because they are learning Perl. Granted, a bunch of people could look at your code and know why it wasn't working, but there are a number of people who aren't in the know. If you post a comment and find the solution before anyone can respond to you, then it is always a good idea, along with your notification that you solved your issue, to let everyone know "what" the solution was and any other pertinent details.

Regards,

Jeff
Jul 23 '07 #3
KevinADC
4,059 Expert 2GB
probably:

(/START/ .. /END/)
Jul 23 '07 #4
numberwhun
3,509 Expert Mod 2GB
**DOH!** I knew that, I was just trying to get him/her into the practice of explaining the solution that they learned about instead of just saying, "Solve,sorry to bug you".
Jul 23 '07 #5
KevinADC
4,059 Expert 2GB
oops, sorry mate.

......
Jul 23 '07 #6
tThis is the solution that I found. I know the code is too big and it can be written in a more simpler way if anyone know the same let me know.

Purpse: To get all the lines between START and END for all files in a directory and store it another file.

Expand|Select|Wrap|Line Numbers
  1. dirname="D:\\Extra\\Per\\Pat";
  2. open(FH3, ">filelist.txt") || die "Can't create the report file";
  3. open(FH4, ">report.txt") || die "Can't create the report file";
  4. unless (opendir (DIR,"$dirname")){
  5.     print "Can't open the directory $dirname \n";
  6.     exit;
  7. }
  8.  
  9. while ($file = readdir(DIR)){
  10.     print FH3 "$file\n";
  11. }
  12. close(FH3);
  13.  
  14. #***************Actual code*********************
  15. $write=0;
  16. open (FH, qq*<filelist.txt*) || die "Can't find the filelist.txt";
  17. while (chomp($mfile = <FH>)) {
  18.     if (-e $mfile && $mfile=~/txt$/) {
  19.         open (FH1,"<$mfile");
  20.         while (<FH1>) {
  21.             if ($_ =~ /start/) {
  22.                 $write=1;
  23.             }
  24.  
  25.             if ($write) {
  26.                 print FH4 "The Filename is $mfile $_";
  27.                 if ($_=~/END/) {
  28.                     $write=0;
  29.                 }
  30.             }
  31.         }
  32.     }
  33. }
  34.  
  35. close(FH4);
  36. close(FH1);
  37.  
Jul 24 '07 #7
KevinADC
4,059 Expert 2GB
Your code could be written differently and improved upon, but there is really nothing terribly wrong with your code. The worst mistake is not using the "strict" and "warnings" pragma.
Jul 24 '07 #8

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

Similar topics

5
by: rxl124 | last post by:
Hi, quick question, how would you assign a contents to file in perl i was thinking $filename=`echo newvalue > filename`;
11
by: Frank B. | last post by:
Is there anyway to write a folders contents to file? -Frank B.
13
Deathwing
by: Deathwing | last post by:
Hello, I'm fairly new to python and have only been playing around with it for one week actually. It's my first attempt at programming and I'm trying to generate some random floats and then...
2
by: jDawg123 | last post by:
Can somone plz give me some ideas on writing a perl script that reads the contents of files from a directory and outputs to an output file the areas in the files read from the directory that have two...
1
by: cdnmr | last post by:
Hi, I am a newcomer to VB, (just about to complete my introductory course). I have developed a feedback form where users can input their personal details (name, telephone etc) as well as feedback...
5
by: grasshopper2 | last post by:
I am looking to build a regular expression that will match pTOC(somenumber) where the number varies between 1& ten, this is to generate a table of contents file that I desperately need to display...
3
by: Nash | last post by:
Hi All, I have set of data which needs to be stored in a file. The data is classified into categories say Cat 1, Cat 2... Cat 10 each category can have any number of entries. The program allows...
2
by: DR | last post by:
how to search tfs by name of file or contents in file?
0
by: FredSovenix | last post by:
Can anybody provide instruction or examples on how to access the noise words file using T-SQL? I want to be able to adjust the users' search string by eliminating the noise words, but if the...
6
by: Feivi18 | last post by:
Hi everyone, new user here. I've been finding it harder and harder to get answers at EE for certain questions, and am hoping someone here will be able to help me! Using asp.net and c#, my site...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.