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: Beginner:
Fetch specific content from file

 



sheet.pangasa
Novice

Jan 11, 2013, 9:10 AM

Post #1 of 10 (1250 views)
Fetch specific content from file Can't Post

Hi All,

I know perl has extremely good string manipulation capabilities and a lot can be done through it. I have a file which gets generated by one process and there is a requirement to fetch specific content from the file and either store the output is a separate file or make the modifications to the same file. The contents of the file are below. I just need to fetch the folder names from it. The folder names will change whenever the process runs but everything above and below it will remain same except the date etc:


Code
 
Informatica(r) PMREP, version [8.6.0], build [178.0613], Windows 32-bit
Copyright (c) Informatica Corporation 1994 - 2008
All Rights Reserved.
This Software may be protected by U.S. Patent Numbers 6,208,990; 6,044,374; 6,014,670; 6,032,158; 5,794,246; 6,339,775; 6,850,947; 6,895,471; 7,254,590 and other U.S. Patents Pending.

Invoked at Fri Jan 11 22:32:09 2013

Connected to repository repo1 in Domain_SheetPangasa-PC as user Administrator
connect completed successfully.

Completed at Fri Jan 11 22:32:14 2013
Successfully connected to repo1

Informatica(r) PMREP, version [8.6.0], build [178.0613], Windows 32-bit
Copyright (c) Informatica Corporation 1994 - 2008
All Rights Reserved.
This Software may be protected by U.S. Patent Numbers 6,208,990; 6,044,374; 6,014,670; 6,032,158; 5,794,246; 6,339,775; 6,850,947; 6,895,471; 7,254,590 and other U.S. Patents Pending.

Invoked at Fri Jan 11 22:32:14 2013

f1
ff4
f5
f8
f9
f10
folder1
folder2
folder3
f2
f3
f6
f7
.listObjects completed successfully.

Completed at Fri Jan 11 22:32:19 2013
Folder list has been fetched successfully for repo1


From the above contents i would want to fetch:


Code
f1 
ff4
f5
f8
f9
f10
folder1
folder2
folder3
f2
f3
f6
f7


Any pointers on how this can be done in perl? I can do this using shell with grep or sed but would really want to do in perl.

thanks,
Sheet


(This post was edited by sheet.pangasa on Jan 11, 2013, 9:17 AM)


FishMonger
Veteran / Moderator

Jan 11, 2013, 10:16 AM

Post #2 of 10 (1244 views)
Re: [sheet.pangasa] Fetch specific content from file [In reply to] Can't Post

What have you tried thus far?

Are you looking for someone to write the script for you or do you just need help troubleshooting your script?

What is your level of experience in Perl?


sheet.pangasa
Novice

Jan 11, 2013, 10:23 AM

Post #3 of 10 (1242 views)
Re: [FishMonger] Fetch specific content from file [In reply to] Can't Post

Thanks for the reply. I am not looking someone to do the work for me. I am very new to perl. I just need a direction on what functions to look for. I can then do some search and come up with a code. If that code does not work for me i would be seeking little help in improving it, not asking anyone to do the actual work for me.

thanks.


sheet.pangasa
Novice

Jan 11, 2013, 10:25 AM

Post #4 of 10 (1240 views)
Re: [FishMonger] Fetch specific content from file [In reply to] Can't Post

right now i am reading about the Translation tr function to see if that can be used.


FishMonger
Veteran / Moderator

Jan 11, 2013, 10:32 AM

Post #5 of 10 (1237 views)
Re: [sheet.pangasa] Fetch specific content from file [In reply to] Can't Post

I don't know what portions of the task you need help with, but here's how I'd begin to approach it and adjust as needed.

Loop/skip over the contents until you reach the "Invoked at .." line.

Then start a new loop which outputs the desired lines until it reaches the ".listObjects completed .." line or which ever line that can be identified as the stopping point.


FishMonger
Veteran / Moderator

Jan 11, 2013, 10:34 AM

Post #6 of 10 (1235 views)
Re: [sheet.pangasa] Fetch specific content from file [In reply to] Can't Post


In Reply To
right now i am reading about the Translation tr function to see if that can be used.


Based on what you've provided so far, I don't see anything that indicates that the tr function would be useful. You might need/want to use a regex.


sheet.pangasa
Novice

Jan 11, 2013, 10:40 AM

Post #7 of 10 (1232 views)
Re: [FishMonger] Fetch specific content from file [In reply to] Can't Post

Yup regex is next in the list... and i was also thinking on the same approach to use loops like you mentioned. I will check and come up with something.

thanks.


sheet.pangasa
Novice

Jan 11, 2013, 11:38 AM

Post #8 of 10 (1228 views)
Re: [FishMonger] Fetch specific content from file [In reply to] Can't Post

did some research and came up with the below code. I know it's not pretty but is doing the job for me and i have the folder names in a separate file. The only problem is i have too many empty/blank line on the top and bottom. Next step is to try and trim that. Tried chomp but then it trims the new line char as well which i don' want.


Code
#!/usr/bin/perl 
use strict;
use warnings;

my ($qfn_in, $qfn_out) = @ARGV;

open(my $fh_in, '<', $qfn_in)
or die("Unable to read file \"$qfn_in\": $!\n");

open(my $fh_out, '>', $qfn_out)
or die("Unable to create file \"$qfn_out\": $!\n");

while (<$fh_in>) {
if (!(/(PMREP|Copyright|All Rights Reserved|Patent|Invoked|Connected to repository|connect completed successfully|Completed at|Successfully|.listObjects|fetched)/)) {
print $fh_out $_;
}
}


Output

Code
 






f1
ff4
f5
f8
f9
f10
folder1
folder2
folder3
f2
f3
f6
f7



FishMonger
Veteran / Moderator

Jan 11, 2013, 11:42 AM

Post #9 of 10 (1226 views)
Re: [sheet.pangasa] Fetch specific content from file [In reply to] Can't Post

Add this before the if block.

Code
next if /^\s*$/;



FishMonger
Veteran / Moderator

Jan 11, 2013, 11:56 AM

Post #10 of 10 (1223 views)
Re: [sheet.pangasa] Fetch specific content from file [In reply to] Can't Post

This is more verbose, but is a lot more efficient than using your regex.


Code
#!/usr/bin/perl 

use strict;
use warnings;

my $invoked = 0;

LINE: while (<DATA>) {
next if /^\s*$/;
if ( $invoked == 2 ) {
while (<DATA>) {
next if /^\s*$/;
last LINE if /^.listObjects completed/;
print;
}
}
$invoked++ if /^Invoked/;
}


__DATA__
Informatica(r) PMREP, version [8.6.0], build [178.0613], Windows 32-bit
Copyright (c) Informatica Corporation 1994 - 2008
All Rights Reserved.
This Software may be protected by U.S. Patent Numbers 6,208,990; 6,044,374; 6,014,670; 6,032,158; 5,794,246; 6,339,775; 6,850,947; 6,895,471; 7,254,590 and other U.S. Patents Pending.

Invoked at Fri Jan 11 22:32:09 2013

Connected to repository repo1 in Domain_SheetPangasa-PC as user Administrator
connect completed successfully.

Completed at Fri Jan 11 22:32:14 2013
Successfully connected to repo1

Informatica(r) PMREP, version [8.6.0], build [178.0613], Windows 32-bit
Copyright (c) Informatica Corporation 1994 - 2008
All Rights Reserved.
This Software may be protected by U.S. Patent Numbers 6,208,990; 6,044,374; 6,014,670; 6,032,158; 5,794,246; 6,339,775; 6,850,947; 6,895,471; 7,254,590 and other U.S. Patents Pending.

Invoked at Fri Jan 11 22:32:14 2013

f1
ff4
f5
f8
f9
f10
folder1
folder2
folder3
f2
f3
f6
f7
.listObjects completed successfully.

Completed at Fri Jan 11 22:32:19 2013
Folder list has been fetched successfully for repo1


 
 


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

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