LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-08-2004, 07:17 PM   #1
true_atlantis
Member
 
Registered: Oct 2003
Distribution: fedora cor 5 x86_64
Posts: 639

Rep: Reputation: 30
perl regular expression problem


i have a perl script that i created, and what it needs to do is process a bunch of files and put the info from those files into a different format into a summary file. when processing, i need to take out all of the <br> and i come into a problem. i use this

foreach $line (@fields_add){
$line =~ s/<br>/ /i;
}

to replace the <br> with a space, but it does not get all of them.
basically if you look at the perl script, what it does is just seperate the lines of the files at the occurance of ;; and then puts them in a format for another script. so, if you look at where it shows

1/2<br>This run is too short to be a segment;;

that <br> will be removed, but if you look at

<br>Lat/lons are for start and stop of imaging;;

it will skip that <br>... anyone know why this is not working correctly???



file to be processed 'f000411t01.log' :

f000411t01;;p01_r01;;1;;1;;Preflight;;;;Jack Holbrook;;;;;;;;;;1738;;1740;;4 - Pre/Postflight;;;;
f000411t01;;p02_r01;;2;;1;;Mobile Check;;;;Dee Porter;;;;;;;;;;19:07:09;;19:09:01;;6 - Tarmac Check;;;;
f000411t01;;p03_r01;;3;;1;;Maui 3-A, HI;;02072;;Gene Carl Feldman;;20-49.6;;157-8.6;;20-44.1;;156-22.7;;20:26:51;;20:34:29;;1 - Science;;28/27;;
f000411t01;;p03_r02;;3;;2;;Molokai 2-A, HI;;02072;;Gene Carl Feldman;;21-5.6;;156-42.2;;21-10.3;;157-20.8;;20:41:41;;20:47:53;;1 - Science;;97/98;;
f000411t01;;p03_r03;;3;;3;;MODIS/MOBY;;02026;;Green;;20-53.55;;157-5.1;;20-36.9;;157-5.4;;21:06:30;;21:19:53;;1 - Science;;Control point = 20-49.6, 157-08.25<br>Looking at MOBY @ approx. 21:14:33<br>Lat/lons are for start and stop of imaging;;
f000411t01;;p03_r04;;3;;4;;Hawaii 4-A, HI;;02072;;Gene Carl Feldman;;20-0.9;;156-0.3;;19-59.0;;155-42.1;;21:31:15;;21:35:01;;1 - Science;;58/57;;
f000411t01;;p03_r05;;3;;5;;Hawaii 5-B, HI;;02072;;Gene Carl Feldman;;19-54.6;;155-47.6;;19-56.5;;156-5.3;;21:40:52;;21:44:21;;1 - Science;;60/59<br>5-A was flown on 000410;;
f000411t01;;p03_r06;;3;;6;;Oahu 4-A, HI;;02072;;Gene Carl Feldman;;21-18.0;;157-45.8;;21-19.8;;158-18.1;;22:03:02;;22:06:03;;1 - Science;;7/8;;
f000411t01;;p03_r07;;3;;7;;Oahu 3-A, HI;;02072;;Gene Carl Feldman;;21-25.8;;158-9.3;;21-23.9;;157-53.4;;22:11:37;;22:15:00;;1 - Science;;5/6;;
f000411t01;;p03_r08;;3;;8;;Oahu 1-A, HI;;02072;;Gene Carl Feldman;;21-27.8;;157-45.7;;21-29.9;;158-3.0;;22:20:21;;22:23:47;;1 - Science;;1/2<br>This run is too short to be a segment;;
f000411t01;;p03_r09;;3;;9;;Niihau 2-A, HI;;02072;;Gene Carl Feldman;;21-49.7;;159-59.4;;21-52.1;;160-16.9;;22:40:03;;22:43:29;;1 - Science;;47/48;;
f000411t01;;p03_r10;;3;;10;;Kauai 5-A, HI;;02072;;Gene Carl Feldman;;21-59.5;;160-3.5;;21-54.7;;159-29.0;;22:51:15;;22:57:22;;1 - Science;;44/43;;
f000411t01;;p04_r01;;4;;1;;Postflight;;;;Chuck Sarture;;;;;;;;;;00:40:00;;00:42:00;;4 - Pre/Postflight;;;;




perl script 'convert.pl' :
Code:
#!/usr/bin/perl
$LID    = 100;
$FLT    = 0;
$F_PATH = 1;
$LAT1   = 7;
$LON1   = 8;
$LAT2   = 9;
$LON2   = 10;

@filelist  = <f*.log>;
#@filelist = <f000411t01.log>;

$arc_file  = "Arc_file";
$info_file = "info_file";
%out_dat = ();
$hide = 0;

if($ARGV[0] eq "-h" || $ARGV[0] eq "-H"){
	$hide = 1;
}

open (OUTPUT, ">$arc_file") || die "Cant create $arc_file";


print($a);

foreach $F (@filelist){
	if($hide == 0){
		print("Processing file $F...\n");
	}
	open (FILE, $F) || die "Cant open $arg_file";
	while ($line = <FILE>) {
		@fields = split(/;;/, $line);
		if($fields[$LAT1]){
			$LID++;
			#first latitude and longitude
			($lat1_d, $lat1_m) = split(/-/, $fields[$LAT1]);
			$lat1_d += $lat1_m/60;
			($lon1_d, $lon1_m) = split(/-/, $fields[$LON1]);
			$lon1_d = ($lon1_d * -1) + (($lon1_m/60) * -1);
			
			#second lat and lon
			($lat2_d, $lat2_m) = split(/-/, $fields[$LAT2]);
			$lat2_d += $lat2_m/60;
			($lon2_d, $lon2_m) = split(/-/, $fields[$LAT2]);
			$lon2_d = ($lon2_d * -1) + (($lon2_m/60) * -1);

			#output to lat/lon file
			print (OUTPUT "$LID\n");
			print (OUTPUT "$lon1_d\t$lat1_d\n");
			print (OUTPUT "$lon2_d\t$lat2_d\n");
			print (OUTPUT "end\n");

			#store the rest of the info into a hash
			$UID = "$fields[0]_$fields[1]";

			@fields_add = @fields[1 .. 6, 11 .. $#fields];

			#take out the <br>
			foreach $line (@fields_add){
				$line =~ s/<br>/ /i;
			}
						
			$out_dat{$LID} = (join("\t", $UID, join("\t", @fields_add)));
		}
	}
close(FILE);
}
print (OUTPUT "end\n");
close(OUTPUT);
open(OUTPUT, ">$info_file");
	
for $key (sort {$a<=>$b} keys %out_dat){
	print(OUTPUT "$key\t$out_dat{$key}\n");
}

Last edited by true_atlantis; 01-09-2004 at 04:46 AM.
 
Old 01-09-2004, 12:59 AM   #2
eric.r.turner
Member
 
Registered: Aug 2003
Location: Planet Earth
Distribution: Linux Mint
Posts: 216

Rep: Reputation: 31
Re: perl regular expression problem

Quote:
Originally posted by true_atlantis

foreach $line (@fields_add){
$line =~ s/<br>/ /i;
}

anyone know why this is not working correctly???
Yeah, it should be:

Code:
foreach $line (@fields_add){
   $line =~ s/<br>/ /ig;
}
The g option means do a global replace. The line of data that you're having problems with has more than one <br> in it. Without the g option the regex only matches the first instance of <br>.

Regards,
Eric

Last edited by eric.r.turner; 01-09-2004 at 03:02 PM.
 
Old 05-27-2009, 12:41 AM   #3
thangappan
Member
 
Registered: May 2009
Posts: 52

Rep: Reputation: 16
Arrow Don't confuse

Perl is a very effective and simple programming language.

I solution is using 'g' in the substitution it can be solved.

while (<$FH>){
if ($_ =~ s/<br>//g){
print "$_\n";
}
}

Input:
testing <br> testing <br>
<br>testing <br> testing

Output:
testing testing
testing testing
 
Old 05-27-2009, 01:00 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,286

Rep: Reputation: 4165Reputation: 4165Reputation: 4165Reputation: 4165Reputation: 4165Reputation: 4165Reputation: 4165Reputation: 4165Reputation: 4165Reputation: 4165Reputation: 4165
That's a *five* year old thread ....
 
Old 05-27-2009, 07:35 AM   #5
eric.r.turner
Member
 
Registered: Aug 2003
Location: Planet Earth
Distribution: Linux Mint
Posts: 216

Rep: Reputation: 31
Quote:
Originally Posted by syg00 View Post
That's a *five* year old thread ....
And he didn't provide any new information! Weird.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl regular expression issue zikhermm Programming 7 09-23-2005 04:48 PM
Having trouble with a perl regular expression... jayemef Programming 3 08-26-2005 12:00 AM
Perl Regular Expression dilemma GATTACA Programming 1 03-27-2004 08:48 PM
Perl Regular Expression rch Programming 14 07-12-2003 12:00 AM
using a perl regular expression in php markus1982 Programming 5 11-18-2002 03:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration