CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Need Help Parsing a file

 



freddie
Novice

May 28, 2009, 12:36 PM

Post #1 of 14 (2055 views)
Need Help Parsing a file Can't Post

I need a little routine in Perl and for the life of me I can't seem to get it right. Here's the situation:

I have a flat text file that looks like this:



[unknown number of lines of text(a)]
<!--begin-->
[unknown number of lines of text(b)]
<!--end postings-->
[unknown number of lines of text(c)]



I want to open the file,

strip [lines of text(a)]

keep:

<!--begin-->
[lines of text(b)]
<!--end postings-->

strip [lines of text(c)]

and rewrite the file.

Any help?


(This post was edited by freddie on May 28, 2009, 12:53 PM)


alex_v2
User

May 28, 2009, 12:54 PM

Post #2 of 14 (2050 views)
Re: [freddie] Need Help Parsing a file [In reply to] Can't Post

I like other here have written many thing like this in the past. Please show us what you have tried and we will be happy to help.

-Alexander


KevinR
Veteran


May 28, 2009, 1:14 PM

Post #3 of 14 (2047 views)
Re: [alex_v2] Need Help Parsing a file [In reply to] Can't Post

Or he can read the FAQs forum.....

http://www.perlguru.com/gforum.cgi?post=9166
-------------------------------------------------


freddie
Novice

May 28, 2009, 1:18 PM

Post #4 of 14 (2047 views)
Re: [alex_v2] Need Help Parsing a file [In reply to] Can't Post

Nothing I tried even came close to the objective. I can't even 'get close enough' to check the variables of the first step. I simply can't see the forrest due to the trees.


(This post was edited by freddie on May 28, 2009, 1:27 PM)


alex_v2
User

May 28, 2009, 1:35 PM

Post #5 of 14 (2043 views)
Re: [KevinR] Need Help Parsing a file [In reply to] Can't Post

I do it a lot different then that but it will get you headed in the right direction.

Later when I get some time I'll do a small version of what I would do and at least give you a starting point.

-Alexander


freddie
Novice

May 28, 2009, 1:36 PM

Post #6 of 14 (2043 views)
Re: [KevinR] Need Help Parsing a file [In reply to] Can't Post

OK, I'm a Donkie! I read the examples in the post and am STILL lost. I thank you for your time. I sincerely believe you thought your reply was helpful but I simply don't 'get it'.


freddie
Novice

May 28, 2009, 1:42 PM

Post #7 of 14 (2041 views)
Re: [alex_v2] Need Help Parsing a file [In reply to] Can't Post

Alexander, Whew! I look forward to your reply. Thanks.


KevinR
Veteran


May 28, 2009, 1:44 PM

Post #8 of 14 (2039 views)
Re: [freddie] Need Help Parsing a file [In reply to] Can't Post


In Reply To
OK, I'm a Donkie! I read the examples in the post and am STILL lost. I thank you for your time. I sincerely believe you thought your reply was helpful but I simply don't 'get it'.


Oh well, hopefully you don't hold it against me or call me names for trying to help. Your inability to understand the examples is out of my control. Wink
-------------------------------------------------


freddie
Novice

May 28, 2009, 2:04 PM

Post #9 of 14 (2036 views)
Re: [KevinR] Need Help Parsing a file [In reply to] Can't Post

>Oh well, hopefully you don't hold it against me or call me names for trying to help. Your inability to understand the examples is out of my control

I didn't intend to insult you. It is obvious that your knowledge and experience exceeds my level and as I said, I sincerely believe you thought you were being helpful but when someone asks directions to the local Walmart and you provide a World Atlas - that isn't very helpful. You must realize that simply by reading my initial question I'm just a novice trying to learn.

Again, thank you for your time.


(This post was edited by freddie on May 28, 2009, 2:04 PM)


KevinR
Veteran


May 29, 2009, 9:10 AM

Post #10 of 14 (2022 views)
Re: [freddie] Need Help Parsing a file [In reply to] Can't Post

I was not offended. Glad you weren't either. If you are a novice, why are you posting in the Intermediate forum instead of the Beginners forum?
-------------------------------------------------


(This post was edited by KevinR on May 29, 2009, 9:11 AM)


alex_v2
User

May 29, 2009, 3:31 PM

Post #11 of 14 (2012 views)
Re: [KevinR] Need Help Parsing a file [In reply to] Can't Post

Kevin is right this is pretty much a beginner thread not really intermediate. Sorry it took so long but I wanted to take some time with this one. I think most people starting with perl are doing so mostly because of the need for file parsing of some kind.

I have been very fortunate in my career to work with some of the greatest perl people in the world. I also for about 5 years had one of the best perl mentors one could ask for. He liked teaching me because he would show me one small example and I would bring him back some really big use of the small thing he showed me. So in some cases I know it is harder trying to find a place to start. Since this is a common thing people do with perl I thought I would right up almost a lesson type deal and hope the other knowledgeable people on the forum will chime in.

Because as we know there is about 100 different ways to 1 thing in perl and file parsing has about 1,000,000 different ways. So first I will show you the script I would use and then I will break it down. Hope this helps some people starting out in perl.


Code
#!/usr/bin/perl 

use strict;

open(FILE, "<input.txt") || die $!;
my ($data, $start);

while ( my $line = <FILE> ) {
if ( $line =~ m/<!--\s*begin\s*-->/i ) {
$start = 1;
next;
}

if ( $line =~ m/<!--\s*end\s*postings\s*-->/i ) {
$start = 0;
next;
}

if ( $start == 1 ) {
$data .= $line;
next;
}
}

close(FILE);


Ok just had something come up so will have to break the code down later plus I am sure the post has enough of my ramblings. Other perl gurus feel free to pick it apart.

-Alexander


(This post was edited by alex_v2 on May 29, 2009, 3:43 PM)


KevinR
Veteran


May 29, 2009, 5:37 PM

Post #12 of 14 (2004 views)
Re: [alex_v2] Need Help Parsing a file [In reply to] Can't Post

Since the cat is sort of out of the bag.....

When you want to edit a file inplace, use perls inplace editor Smile


Code
{ 
local @ARGV = ('path/to/file.txt');
local $^I = '.bak';#<--invokes inplace editor and creates a backup of the original file with a .bak extension
OUTTER: while (<>) {<- opens and reads the file(s) in @ARGV
if (/\Q<!--begin-->\E/) {
print;
while (<>) {
if (/\Q<!--end postings-->\E/) {
print;
next OUTTER;#<-- return to the first "while" loop
}
else {
print;
}
}
}
}
}


Nothing wrong with Alexanders code, and its proably more understandable for a beginner although the inplace editor is simple to use and probably easier once you get the hang of it. Its also very fast and can be used fromthe command line with the -i switch.
-------------------------------------------------


freddie
Novice

May 29, 2009, 7:56 PM

Post #13 of 14 (2001 views)
Re: [alex_v2] Need Help Parsing a file [In reply to] Can't Post

Kevin & Alexander,

I thank you both for your assistance and efforts.


alex_v2
User

May 29, 2009, 10:16 PM

Post #14 of 14 (1997 views)
Re: [KevinR] Need Help Parsing a file [In reply to] Can't Post

Nice Kevin that will give me a lot to read up on. There is a whole bunch going on there that I have never used before.

-Alexander

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives