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:
Parsing Text File -- Troubleshooting Help

 



xmaverick
User

Jan 12, 2010, 8:48 AM

Post #1 of 21 (2298 views)
Parsing Text File -- Troubleshooting Help Can't Post

I'm trying to figure out why it is that when i print STDOUT my data does not match with what i dump into a text file.

Basically my script does the following:

- telnet into a network node and creates a log file in the form of a text format.
- The log file is then opened up and dumped into an array.
- The array is then checked for certain patterns.
- The pattern is then dumped into another array.
- The array with the pattern is printed to STDOUT just to check

- The array is dumped into another file.


When i compare the output of STDOUT with the data dumped into another text file there is only one line of data.

Has any one every experienced this problem? Below is a snip of my code.

open (FileRead, '/tmp/input.txt');
while (<FileRead>) {
my @output = <FileRead>;

foreach my $i (@output){
if ($i =~ m/UL:/){

my @line = $i;

open (FileWrite, '>/tmp/input2.txt');
print FileWrite "@line\n";
close (FileWrite);

}
}
}

close (FileRead);
print "$time seconds of data was sampled \n";


toolic
User

Jan 12, 2010, 10:18 AM

Post #2 of 21 (2295 views)
Re: [xmaverick] Parsing Text File -- Troubleshooting Help [In reply to] Can't Post

This looks bad:

Code
while (<FileRead>) { 
my @output = <FileRead>;


Every time you use <FileRead>, you read a new file
into your script, essentially clobbering a previous line.

I don't understand what you are trying to do.
Can you post a small sample of your input file,
and what you want your output file to contain?


xmaverick
User

Jan 12, 2010, 10:47 AM

Post #3 of 21 (2294 views)
Re: [toolic] Parsing Text File -- Troubleshooting Help [In reply to] Can't Post


Code
#!/usr/bin/perl -w 

use strict;
use Net::Telnet;

my @final;
my $finalResult;
my $result2;
my $result;
my $line;
my $time = $ARGV[1];
my $ip = $ARGV[0];
my $t=new Net::Telnet(Errmode=>'return',
Input_Log => "/tmp/input.txt",
Timeout => 20 );

$t->open("10.156.$ip.3");
if($t->errmsg) {my $err=$t->errmsg; return $err;}

$t->waitfor('/->\s*$/');
$t->cmd(String=>'ho_log_on_bts',Prompt=>'/->\s*$/');
sleep("$time");
$t->cmd(String=>'ho_log_off_bts',Prompt=>'/->\s*$/');

$t->close ();

###### the top portion telnet into a network node, executes a command, and
###### collects generates an input.txt.


##### The botton portion simply opens the file which contains data, and looks for
#### a match and if found then places that data into an array. The array then
#### is printed to STDOUT just to compare with the data that is placed on a new txt
#### file. Unfortunately, when i compare the data the text file only contains the last
#### line of what was printed in STDOUT.


open (FileRead, '/tmp/input.txt');
while (<FileRead>) {
my @output = <FileRead>;

foreach my $i (@output){


if ($i =~ m/UL:/){


my @line = $i;
print "@line \n";

open (FileWrite, '>/tmp/input2.txt');
print FileWrite "@line\n";
close (FileWrite);

}
}
}

close (FileRead);
print "$time seconds of data was sampled \n";




The output for STDOUT of the @line array will come out for example as so after it found the match:
1263321659.166 (bts ) (6 0xf)UL:38/38 3/1 F AVG 39 2 DL:26/27 0/0 F AVG 24 0 NBR

1263321659.649 (bts ) (6 0xf)UL:39/39 3/2 F AVG 39 2 DL:28/29 0/0 F AVG 25 0 NBR

1263321660.133 (bts ) (6 0xf)UL:39/39 3/0 F AVG 39 2 DL:28/28 0/0 F AVG 26 0 NBR

1263321660.616 (bts ) (6 0xf)UL:38/36 3/2 F AVG 38 3 DL:26/26 0/0 F AVG 27 0 NBR

1263321661.099 (bts ) (6 0xf)UL:37/37 3/3 F AVG 38 3 DL:26/26 0/0 F AVG 27 0 NBR

1263321661.566 (bts ) (6 0xf)UL:35/35 4/4 F AVG 37 3 DL:26/25 0/0 F AVG 26 0 NBR

1263321662.049 (bts ) (6 0xf)UL:35/33 4/4 F AVG 36 3 DL:26/25 0/0 F AVG 26 0 NBR

1263321662.533 (bts ) (6 0xf)UL:35/35 4/1 F AVG 35 3 DL:27/27 0/0 F AVG 26 0 NBR

1263321663.016 (bts ) (6 0xf)UL:35/34 4/1 F AVG 35 4 DL:27/26 0/0 F AVG 26 0 NBR

1263321663.499 (bts ) (6 0xf)UL:35/35 5/4 F AVG 35 4 DL:25/26 0/0 F AVG 26 0 NBR

1263321663.966 (bts ) (6 0xf)UL:40/41 3/0 F AVG 36 4 DL:26/26 0/0 F AVG 26 0 NBR

1263321664.449 (bts ) (6 0xf)UL:42/42 2/0 F AVG 38 3 DL:26/26 0/0 F AVG 26 0 NBR

1263321664.933 (bts ) (6 0xf)UL:41/41 2/1 F AVG 39 3 DL:25/25 0/0 F AVG 25 0 NBR



The output of the text file will only have contained the last line of STDOUT as so:

1263321664.933 (bts ) (6 0xf)UL:41/41 2/1 F AVG 39 3 DL:25/25 0/0 F AVG 25 0 NBR


toolic
User

Jan 12, 2010, 12:03 PM

Post #4 of 21 (2281 views)
Re: [xmaverick] Parsing Text File -- Troubleshooting Help [In reply to] Can't Post

Can you post a small sample of your input file, /tmp/input.txt?


FishMonger
Veteran / Moderator

Jan 12, 2010, 1:18 PM

Post #5 of 21 (2276 views)
Re: [xmaverick] Parsing Text File -- Troubleshooting Help [In reply to] Can't Post


Code
while (<FileRead>) {  
my @output = <FileRead>;

That throws away the first line of the input file, then slups the rest of it into an array. Is that really what you want?


Code
foreach my $i (@output){

Now you're looping over the data a second time, which is inefficient.


Code
foreach my $i (@output){  


if ($i =~ m/UL:/){


my @line = $i;
print "@line \n";

open (FileWrite, '>/tmp/input2.txt');
print FileWrite "@line\n";


This is where your main problem is located. Apparently you don't fully understand what that code is doing and I don't know how much of it you do understand.

Each time through the loop, if the regex matches, @line is assigned the current line in its first element, overwriting the previous line.

Then you open input2.txt in write mode clobbering anything that was previously in it and then you output the single element array value, which gets overwritten the next time through the loop.


xmaverick
User

Jan 12, 2010, 1:35 PM

Post #6 of 21 (2272 views)
Re: [toolic] Parsing Text File -- Troubleshooting Help [In reply to] Can't Post

Yes, my input file would be the following sample:



Code
HD:bsx_NCL_JADE-> ho_log_on_bts 
Enabling HO tracing for all active calls
value = 41 = 0x29 = ')'
HD:bsx_NCL_JADE-> 1263329603.999 (bts ) (4 0x38)UL:50/50 1/1 F AVG 47 2 DL:22/22 0/0 F AVG 28
1263329604.383 (bts ) (6 0xd)UL:47/46 3/2 F AVG 47 2 DL:37/32 0/0 F AVG 35 0 NBR
1263329604.483 (bts ) (4 0x38)UL:44/44 1/1 F AVG 47 1 DL:35/35 0/0 F AVG 29 0 NBR
1263329604.849 (bts ) (6 0xd)UL:45/46 2/3 F AVG 46 2 DL:32/36 2/0 F AVG 34 1 NBR
1263329604.949 (bts ) (4 0x38)UL:43/43 3/3 F AVG 46 1 DL:34/34 0/0 F AVG 30 0 NBR
1263329605.333 (bts ) (6 0xd)UL:45/45 4/4 F AVG 46 2 DL:36/36 0/0 F AVG 35 0 NBR
1263329605.416 (bts ) (4 0x38)UL:41/41 3/3 F AVG 44 2 DL:34/34 0/0 F AVG 31 0 NBR
1263329605.816 (bts ) (6 0xd)UL:46/45 3/3 F AVG 45 3 DL:39/41 0/0 F AVG 36 0 NBR
1263329605.883 (bts ) (4 0x38)UL:48/48 2/2 F AVG 44 2 DL:39/39 0/0 F AVG 35 0 NBR
1263329606.299 (bts ) (6 0xd)UL:45/45 3/3 F AVG 45 3 DL:42/42 0/0 F AVG 37 0 NBR
1263329606.349 (bts ) (4 0x7d)UL:16/16 7/7 F AVG 20 7 DL: 0/ 0 7/7 F AVG 0 7 NBR
1263329606.366 (bts ) (4 0x38)UL:49/49 0/0 F AVG 45 2 DL:39/39 0/0 F AVG 35 0 NBR
1263329606.649 (BscCall) GetTlliMapEntry:Got the entry 99 for tlli:0xcba6c731
1263329606.783 (bts ) (6 0xd)UL:44/44 4/4 F AVG 45 3 DL:42/41 0/0 F AVG 39 0 NBR
1263329606.833 (bts ) (4 0x7d)UL:15/15 7/7 F AVG 17 7 DL: 0/ 0 7/7 F AVG 0 7 NBR
1263329606.833 (bts ) Configured Criterion Met: CP_L_RXQUAL_UL_H
1263329606.833 (bts ) (4 0x7d): Candidate Rank = 1, Object Id = 0
1263329606.833 (bts ) (4 0x7d): Candidate Rank = 2, Object Id = 1
1263329606.833 (bts ) (4 0x7d): Candidate Rank = 3, Object Id = 2
1263329606.833 (bts ) (4 0x38)UL:27/27 6/6 F AVG 41 2 DL:39/39 0/0 F AVG 35 0 NBR
1263329607.183 (GprsTask) gpTbf->timing_adv_index 0 != tai 1 for TFI 6 - Dir 1
1263329607.249 (bts ) (6 0xd)UL:44/43 4/4 F AVG 44 3 DL:34/38 0/0 F AVG 39 0 NBR
1263329607.299 (bts ) (4 0x38)UL:15/15 7/7 F AVG 34 3 DL:39/39 0/0 F AVG 35 0 NBR
1263329607.733 (bts ) (6 0xd)UL:43/44 4/3 F AVG 44 3 DL:42/43 0/0 F AVG 40 0 NBR
1263329607.766 (bts ) (4 0x38)UL:18/18 7/7 F AVG 27 5 DL:39/39 0/0 F AVG 35 0 NBR
1263329608.216 (bts ) (6 0xd)UL:41/41 4/4 F AVG 43 4 DL:44/45 0/0 F AVG 40 0 NBR
1263329608.699 (bts ) (6 0xd)UL:40/39 5/5 F AVG 42 4 DL:45/45 0/0 F AVG 41 0 NBR
1263329608.816 (GprsTask) BSN 0 is outside the Receive window with V(Q) 1
1263329609.183 (bts ) (6 0xd)UL:40/39 5/5 F AVG 41 4 DL:42/41 0/0 F AVG 43 0 NBR
1263329609.599 (bts ) (4 0x64)UL:53/53 2/2 F AVG 53 2 DL:45/45 0/0 F AVG 44 0 NBR
1263329609.649 (bts ) (6 0xd)UL:47/45 3/4 F AVG 42 4 DL:38/36 0/0 F AVG 42 0 NBR
1263329610.066 (bts ) (4 0x64)UL:52/52 2/2 F AVG 52 1 DL:43/43 0/0 F AVG 44 0 NBR
1263329610.133 (bts ) (6 0xd)UL:49/48 3/4 F AVG 44 4 DL:37/40 0/0 F AVG 40 0 NBR
1263329610.533 (bts ) (4 0x64)UL:51/51 0/0 F AVG 52 1 DL:43/43 0/0 F AVG 44 0 NBR
1263329610.616 (bts ) (6 0xd)UL:45/46 4/4 F AVG 45 3 DL:40/36 0/0 F AVG 39 0 NBR
1263329611.016 (bts ) (4 0x64)UL:51/51 0/0 F AVG 51 1 DL:43/43 0/0 F AVG 43 0 NBR
1263329611.099 (bts ) (6 0xd)UL:47/46 3/3 F AVG 47 3 DL:33/30 3/3 F AVG 37 0 NBR
1263329611.483 (bts ) (4 0x64)UL:51/51 0/0 F AVG 51 0 DL:44/44 0/0 F AVG 43 0 NBR
1263329611.583 (bts ) (6 0xd)UL:46/46 3/2 F AVG 46 3 DL:35/39 1/0 F AVG 36 1 NBR
1263329611.949 (bts ) (4 0x64)UL:50/50 0/0 F AVG 50 0 DL:44/44 0/0 F AVG 43 0 NBR
1263329612.049 (bts ) (6 0xd)UL:50/49 3/4 F AVG 47 3 DL:34/29 3/3 F AVG 35 1 NBR
1263329612.416 (bts ) (4 0x64)UL:50/50 1/1 F AVG 50 0 DL:44/44 0/0 F AVG 43 0 NBR
1263329612.533 (bts ) (6 0xd)UL:54/54 1/0 F AVG 49 2 DL:38/41 0/0 F AVG 35 1 NBR
1263329612.899 (bts ) (4 0x64)UL:47/47 1/1 F AVG 49 0 DL:44/44 0/0 F AVG 44 0 NBR
1263329613.016 (bts ) (6 0xd)UL:54/54 2/0 F AVG 51 2 DL:37/40 0/0 F AVG 36 1 NBR
1263329613.366 (bts ) (4 0x64)UL:47/47 0/0 F AVG 48 0 DL:45/45 0/0 F AVG 44 0 NBR
1263329613.499 (bts ) (6 0xd)UL:52/52 2/0 F AVG 52 2 DL:41/39 0/0 F AVG 37 0 NBR
1263329613.833 (bts ) (4 0x64)UL:49/49 0/0 F AVG 48 0 DL:46/46 0/0 F AVG 44 0 NBR
1263329613.983 (bts ) (6 0xd)UL:54/54 2/0 F AVG 53 1 DL:37/37 0/0 F AVG 38 0 NBR
1263329614.299 (bts ) (4 0x64)UL:51/51 0/0 F AVG 48 0 DL:44/44 0/0 F AVG 44 0 NBR
1263329614.449 (bts ) (6 0xd)UL:54/55 2/0 F AVG 53 2 DL:39/44 2/0 F AVG 38 0 NBR
1263329614.566 (GprsTask) Wrong CS-1 len 13:
1263329614.783 (bts ) (4 0x64)UL:49/49 2/2 F AVG 49 0 DL:42/42 0/0 F AVG 44 0 NBR
1263329614.933 (bts ) (6 0xd)UL:51/52 2/1 F AVG 52 2 DL:46/44 0/0 F AVG 40 0 NBR
1263329615.249 (bts ) (4 0x64)UL:44/44 2/2 F AVG 48 1 DL:42/42 0/0 F AVG 44 0 NBR
1263329615.416 (bts ) (6 0xd)UL:52/52 0/0 F AVG 52 1 DL:43/44 0/0 F AVG 41 0 NBR
1263329615.716 (bts ) (4 0x64)UL:33/33 6/6 F AVG 44 2 DL:42/42 0/0 F AVG 44 0 NBR
1263329615.816 (gprsrelay) ***** GrBssGpEvent: PAGING PS PDU Recd ****
1263329615.899 (bts ) (6 0xd)UL:52/51 3/3 F AVG 52 1 DL:44/43 0/0 F AVG 43 0 NBR
1263329616.183 (bts ) (4 0x64)UL:17/17 7/7 F AVG 35 4 DL:42/42 0/0 F AVG 44 0 NBR
1263329616.383 (bts ) (6 0xd)UL:53/53 2/0 F AVG 52 1 DL:42/39 0/0 F AVG 43 0 NBR
1263329616.666 (bts ) (4 0x64)UL:16/16 7/7 F AVG 27 5 DL:42/42 0/0 F AVG 44 0 NBR
1263329616.849 (bts ) (6 0xd)UL:51/52 3/1 F AVG 52 2 DL:41/42 0/0 F AVG 42 0 NBR
1263329617.333 (bts ) (6 0xd)UL:47/46 4/2 F AVG 50 3 DL:44/43 0/0 F AVG 42 0 NBR
1263329617.499 (GprsTask) GetLengthAccCap() Unknown access_type 15 in RA_CAPABILITY!
1263329617.499 (GprsTask) GetMcapStruct() returning NULL
1263329617.499 (GprsTask) Unknown access_type 15 in RA_CAPABILITY!
1263329617.816 (bts ) (6 0xd)UL:46/46 4/2 F AVG 49 3 DL:42/42 0/0 F AVG 42 0 NBR
1263329618.299 (bts ) (6 0xd)UL:48/47 4/3 F AVG 48 3 DL:41/41 0/0 F AVG 42 0 NBR
1263329618.783 (bts ) (6 0xd)UL:50/50 2/1 F AVG 47 3 DL:42/43 0/0 F AVG 42 0 NBR
1263329619.133 (BscCall) GetTlliMapEntry:Got the entry 99 for tlli:0xf168ad21
1263329619.249 (bts ) (6 0xd)UL:50/50 2/0 F AVG 48 3 DL:42/40 0/0 F AVG 41 0 NBR
1263329619.733 (bts ) (6 0xd)UL:55/53 1/3 F AVG 50 2 DL:39/39 0/0 F AVG 41 0 NBR
1263329620.216 (bts ) (6 0xd)UL:56/56 0/0 F AVG 52 1 DL:36/37 0/0 F AVG 39 0 NBR
1263329620.233 (GprsTask) BSN 26 is outside the Receive window with V(Q) 30
1263329620.249 (GprsTask) BSN 27 is outside the Receive window with V(Q) 30
1263329620.266 (GprsTask) BSN 28 is outside the Receive window with V(Q) 30
1263329620.283 (GprsTask) BSN 29 is outside the Receive window with V(Q) 30
1263329620.333 (GprsTask) Already received sequence #31. Dumping packet!
1263329620.349 (GprsTask) BSN 26 is outside the Receive window with V(Q) 30
1263329620.366 (GprsTask) BSN 27 is outside the Receive window with V(Q) 30
1263329620.383 (GprsTask) BSN 28 is outside the Receive window with V(Q) 30
1263329620.416 (GprsTask) BSN 29 is outside the Receive window with V(Q) 30
1263329620.449 (GprsTask) Already received sequence #31. Dumping packet!
1263329620.466 (GprsTask) BSN 26 is outside the Receive window with V(Q) 30
1263329620.483 (GprsTask) BSN 27 is outside the Receive window with V(Q) 30
1263329620.533 (GprsTask) BSN 28 is outside the Receive window with V(Q) 30
1263329620.549 (GprsTask) BSN 29 is outside the Receive window with V(Q) 30
1263329620.699 (bts ) (6 0xd)UL:56/56 0/2 F AVG 54 0 DL:36/33 2/1 F AVG 38 0 NBR
1263329620.899 (bts ) (4 0x63)UL:44/44 6/6 F AVG 54 2 DL:46/46 0/0 F AVG 51 0 NBR
1263329620.933 (bts ) (4 0x73)UL:43/43 1/1 F AVG 41 0 DL:27/27 0/0 F AVG 27 0 NBR
1263329621.183 (bts ) (6 0xd)UL:56/56 0/0 F AVG 55 0 DL:39/43 0/0 F AVG 37 0 NBR
1263329621.399 (bts ) (4 0x73)UL:44/44 1/1 F AVG 43 0 DL:27/27 0/0 F AVG 27 0 NBR
1263329621.649 (bts ) (4 0x5d)UL:57/57 0/0 F AVG 58 0 DL:50/50 0/0 F AVG 51 0 NBR
1263329621.649 (bts ) (6 0xd)UL:50/51 3/1 F AVG 54 0 DL:48/48 0/0 F AVG 39 0 NBR
1263329621.866 (bts ) (4 0x73)UL:35/35 5/5 F AVG 41 1 DL:27/27 0/0 F AVG 27 0 NBR
1263329622.133 (bts ) (4 0x5d)UL:57/57 0/0 F AVG 57 0 DL:52/52 0/0 F AVG 51 0 NBR
1263329622.133 (bts ) (6 0xd)UL:51/50 2/1 F AVG 53 1 DL:47/46 0/0 F AVG 42 0 NBR
1263329622.349 (bts ) (4 0x73)UL:13/13 7/7 F AVG 33 3 DL:27/27 0/0 F AVG 27 0 NBR
1263329622.599 (bts ) (4 0x5d)UL:51/51 0/0 F AVG 55 0 DL:51/51 0/0 F AVG 51 0 NBR
1263329622.616 (bts ) (6 0xd)UL:49/50 3/3 F AVG 51 2 DL:45/41 0/0 F AVG 44 0 NBR
1263329622.816 (bts ) (4 0x73)UL:13/13 7/7 F AVG 26 5 DL:27/27 0/0 F AVG 27 0 NBR
1263329623.033 (BscCall) GetTlliMapEntry:Got the entry 99 for tlli:0xf168ad21
1263329623.066 (bts ) (4 0x5d)UL:44/44 2/2 F AVG 52 0 DL:51/51 0/0 F AVG 51 0 NBR
1263329623.099 (bts ) (6 0xd)UL:50/51 3/3 F AVG 50 2 DL:44/45 0/0 F AVG 46 0 NBR
1263329623.199 (BscCall) GetTlliMapEntry:Got the entry 100 for tlli:0xeb79590a
1263329623.283 (bts ) (4 0x73)UL:15/15 7/7 F AVG 19 6 DL:27/27 0/0 F AVG 27 0 NBR
1263329623.283 (bts ) Configured Criterion Met: CP_L_RXQUAL_UL_H
1263329623.283 (bts ) Configured Criterion Met: CP_UL_IH
1263329623.283 (bts ) (4 0x73): Candidate Rank = 1, Object Id = 0
1263329623.283 (bts ) (4 0x73): Candidate Rank = 2, Object Id = 1
1263329623.283 (bts ) (4 0x73): Candidate Rank = 3, Object Id = 2
1263329623.533 (bts ) (4 0x5d)UL:47/47 3/3 F AVG 49 1 DL:51/51 0/0 F AVG 51 0 NBR
1263329623.583 (bts ) (6 0xd)UL:47/47 4/2 F AVG 49 3 DL:45/45 0/0 F AVG 45 0 NBR


The code is suppose to filter everything out except for lines that contain m/UL:/


xmaverick
User

Jan 12, 2010, 1:39 PM

Post #7 of 21 (2271 views)
Re: [FishMonger] Parsing Text File -- Troubleshooting Help [In reply to] Can't Post


Code
 
I altered the code as such but get the same results:

open(FILE, "</tmp/input.txt");
my @lines = <FILE>;
close(FILE);

foreach my $i (@lines) {

chomp ($i);
if ($i =~ m/UL:/){

my @line2 = $i;
print "@line2 \n";

open (FileWrite, '>/tmp/input2.txt');
print FileWrite "@line2\n";
close (FileWrite);

}
}



This looks to be efficient what do you think?


(This post was edited by xmaverick on Jan 12, 2010, 1:39 PM)


xmaverick
User

Jan 12, 2010, 1:49 PM

Post #8 of 21 (2267 views)
Re: [FishMonger] Parsing Text File -- Troubleshooting Help [In reply to] Can't Post

How can i then alter the code to search to m/UL:/ in the array that contains input.txt data to then place it in input2.txt?


I'm simply trying to filter out junk in the input.txt data to then place the data i want into input2.txt.


FishMonger
Veteran / Moderator

Jan 12, 2010, 1:54 PM

Post #9 of 21 (2265 views)
Re: [xmaverick] Parsing Text File -- Troubleshooting Help [In reply to] Can't Post

That "solves" the throwing away of the first line in the input file, but you didn't do anything about the other problems I mentioned.

Please reread my prior post and adjust your script accordingly. If you need additional clarification, then post back and let me know what you don't understand.


FishMonger
Veteran / Moderator

Jan 12, 2010, 1:56 PM

Post #10 of 21 (2264 views)
Re: [xmaverick] Parsing Text File -- Troubleshooting Help [In reply to] Can't Post


In Reply To
How can i then alter the code to search to m/UL:/ in the array that contains input.txt data to then place it in input2.txt?


I'm simply trying to filter out junk in the input.txt data to then place the data i want into input2.txt.



Code
perl -ne 'print if /UL:/' < /tmp/input.txt > /tmp/input2.txt'



xmaverick
User

Jan 12, 2010, 3:37 PM

Post #11 of 21 (2260 views)
Re: [FishMonger] Parsing Text File -- Troubleshooting Help [In reply to] Can't Post


Code
foreach my $i (@output){   


if ($i =~ m/UL:/){


my @line = $i;
print "@line \n";

open (FileWrite, '>/tmp/input2.txt');
print FileWrite "@line\n";



In this portion of the script what i thought i was doing was searching through the index of the @output array. Once it found the m/UL:/ pattern it printed all the lines in the array containing that pattern into @line array. Then, i would take the @line results and print them into input2.txt.


If what your saying is so that the @line array is being overwritten as the m/UL:/ pattern is found in the @output array. How can i prevent the @line from being overwritten as the @output array is searched for the m/UL:/ pattern.

I saw your example but isn't there over ways. I'd like to use arrays if possible.


FishMonger
Veteran / Moderator

Jan 12, 2010, 3:48 PM

Post #12 of 21 (2258 views)
Re: [xmaverick] Parsing Text File -- Troubleshooting Help [In reply to] Can't Post

You don't need any of those arrays.

Open your input file in read mode.
Open your output file in write mode.

Then, as you loop over the lines on the input file, do your regex test and output that line if it matches.

Hint: the while loop consists of only a single statement.


xmaverick
User

Jan 13, 2010, 3:32 PM

Post #13 of 21 (2247 views)
Re: [FishMonger] Parsing Text File -- Troubleshooting Help [In reply to] Can't Post


Code
 
Are you referring to something like this:

open(FILE, "</tmp/input.txt");
while ($lines = <FILE>){

$a = $lines if $lines =~ m/UL:/;


print "$a \n";

open (FileWrite, '>/tmp/input2.txt');
print FileWrite "$a";
close (FileWrite);


}
close(FILE);



FishMonger
Veteran / Moderator

Jan 13, 2010, 3:56 PM

Post #14 of 21 (2245 views)
Re: [xmaverick] Parsing Text File -- Troubleshooting Help [In reply to] Can't Post

That's the section I'm talking about, but you're still making the exact same mistake. DO NOT open the output file inside of the while loop!

I hope this isn't your homework assignment that I'm about to do for you.


Code
# use vars for your filenames 
my $input = '/tmp/input.txt';
my $output = '/tmp/outpu.txt';

# use a lexical var for the filehandle instead of the barword
# and use the 3 arg form of open
# and add error handling
open my $in_fh, '<', $input or die "can't open <$input> $!";
open my $out_fh, '>', $output or die "can't open <$output> $!";


while ( <$in_fh> ) {
# you can easily combine the print statement and regex test in a single, simple, easy to read statement.
print $out_fh $_ if /UL:/;
}

close $in_fh;
close $out_fh;



xmaverick
User

Jan 13, 2010, 5:06 PM

Post #15 of 21 (2239 views)
Re: [FishMonger] Parsing Text File -- Troubleshooting Help [In reply to] Can't Post

Oh no this isn't homework. I'm self taught when it comes to perl. I've just been writing simple scripts for certain tasks at my job. I figure i start learning how to parse and so assigned my self a task to explore.

I attempted what you suggested and it worked like a charm. I have been enlighten. Are there any books you suggest on parsing data using perl? I have been looking in amazon but i don't know which are the good books.

You solved a problem for me that has been eating me up inside for 2 weeks. Man! i feel so good now.


xmaverick
User

Jan 15, 2010, 3:05 PM

Post #16 of 21 (2211 views)
Re: [FishMonger] Parsing Text File -- Troubleshooting Help [In reply to] Can't Post

Hey FishMonger,

Have you ever had a problem with not being able to print an array output. For example:


Code
use strict; 
use Net::Telnet;
my $vessel = $ARGV [2];
my $time = $ARGV[1];
my $ip = $ARGV[0];
my $t=new Net::Telnet(Errmode=>'return',
Input_Log => "/tmp/input.txt",
Timeout => 20 );

$t->open("10.156.$ip.3");
if($t->errmsg) {my $err=$t->errmsg; return $err;}

$t->waitfor('/->\s*$/');
$t->cmd(String=>'ho_log_on_bts',Prompt=>'/->\s*$/');
sleep("$time");
my@output$t->cmd(String=>'ho_log_off_bts',Prompt=>'/->\s*$/');

print "@output\n"

$t->close ();



when i attempt to print @output to STDOUT i only get a blank line. Have you ever experienced such a problem?


FishMonger
Veteran / Moderator

Jan 15, 2010, 3:17 PM

Post #17 of 21 (2210 views)
Re: [xmaverick] Parsing Text File -- Troubleshooting Help [In reply to] Can't Post

That code won't compile.

Why are you doing this:

Code
if($t->errmsg) {my $err=$t->errmsg; return $err;}


Do you know what the return function is used for?


xmaverick
User

Jan 15, 2010, 3:20 PM

Post #18 of 21 (2207 views)
Re: [FishMonger] Parsing Text File -- Troubleshooting Help [In reply to] Can't Post

Isn't it suppose return and error message to STDOUT?


FishMonger
Veteran / Moderator

Jan 15, 2010, 3:28 PM

Post #19 of 21 (2206 views)
Re: [xmaverick] Parsing Text File -- Troubleshooting Help [In reply to] Can't Post

You mean like this:

Quote
C:\test>xmaverick.pl
Scalar found where operator expected at C:\test\xmaverick.pl line 16, near "@output$t"
(Missing operator before $t?)
Scalar found where operator expected at C:\test\xmaverick.pl line 20, near "$t"
(Missing semicolon on previous line?)
syntax error at C:\test\xmaverick.pl line 16, near "@output$t"
syntax error at C:\test\xmaverick.pl line 20, near "$t"
Execution of C:\test\xmaverick.pl aborted due to compilation errors.



FishMonger
Veteran / Moderator

Jan 15, 2010, 3:30 PM

Post #20 of 21 (2205 views)
Re: [xmaverick] Parsing Text File -- Troubleshooting Help [In reply to] Can't Post


Code
C:\test>perldoc -f return 
return EXPR
return Returns from a subroutine, "eval", or "do FILE" with the value
given in EXPR. Evaluation of EXPR may be in list, scalar, or
void context, depending on how the return value will be used,
and the context may vary from one execution to the next (see
"wantarray"). If no EXPR is given, returns an empty list in list
context, the undefined value in scalar context, and (of course)
nothing at all in a void context.

(Note that in the absence of an explicit "return", a subroutine,
eval, or do FILE will automatically return the value of the last
expression evaluated.)



xmaverick
User

Jan 15, 2010, 3:44 PM

Post #21 of 21 (2202 views)
Re: [FishMonger] Parsing Text File -- Troubleshooting Help [In reply to] Can't Post

I got rid of it but unfortunately i'm still not able to retrieve an output from the array. Guess i'll have to continue playing with it.

 
 


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

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