Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PERL - Moving line within a file

Status
Not open for further replies.

landhippy

Programmer
Feb 7, 2003
9
GB
I need to parse a HTML file containing an java applet call, that is created by a third-party application, and move a line to the bottom of an applet definition. I am considering using awk or sed but a colleague recommended Perl.

I need to cut the following line and then insert it back into the file at the end of the applet defintion.

1. Find:
sendGIFToFTP("192.168.81.27","user","pass","./report_areas/","111.gif");

2.Remove it and the paste it at bottom of the applet defintion:

sendGIFToFTP("192.168.81.27","user","pass","./report_areas/","111.gif");
'>
</APPLET>

Any ideas or comments would be gratefully received.

 
Read in the whole file to a string, do a regex to replace the current instance of your line to the point that you want it, then write back to the file again. Sincerely,

Tom Anderson
Order amid Chaos, Inc.
 
As you can probably tell I am a complete beginner to Perl, spent about 30 minutes on it yesterday and came up with the following, in the end I needed to move three files to another part of the file:

#!/usr/local/bin/perl
#** Identification Label TIBER
#** Filename swf_graph.perl
#** Author AP
#**
#** Ver Who When What
#** --- --- ---- ----
#** 1.00 AP 10-Feb-03 Original Issue

LINE: while (<>) { #Loop through the file
if ($_ =~ /^sendGIFToFTP/) { #Look for the sendGIFToFTP command
$MoveLine1=$_; #Store the value of first matching line
$_=<>; #Get the next line
$MoveLine2=$_; #Store the value of second matching line
$_=<>; #Get the next line
$MoveLine3=$_; #Store the value of third matching line
next LINE #Once variables are stored, start loop again
}
if ($_ =~ /^'>/) { #Look for the insert point (end of applet def)
print $MoveLine1; #Paste the first line
print $MoveLine2; #Paste the second line
print $MoveLine3; #Paste the third line
}
print $_; #Otherwise print the other lines
}



 
As you can probably tell I am a complete beginner to Perl, spent about 30 minutes on it yesterday and came up with the following, in the end I needed to move three files to another part of the file:

#!/usr/local/bin/perl
#** Identification Label TIBER
#** Filename swf_graph.perl
#** Author AP
#**
#** Ver Who When What
#** --- --- ---- ----
#** 1.00 AP 10-Feb-03 Original Issue

LINE: while (<>) { #Loop through the file
if ($_ =~ /^sendGIFToFTP/) { #Look for the sendGIFToFTP command
$MoveLine1=$_; #Store the value of first matching line
$_=<>; #Get the next line
$MoveLine2=$_; #Store the value of second matching line
$_=<>; #Get the next line
$MoveLine3=$_; #Store the value of third matching line
next LINE #Once variables are stored, start loop again
}
if ($_ =~ /^'>/) { #Look for the insert point (end of applet def)
print $MoveLine1; #Paste the first line
print $MoveLine2; #Paste the second line
print $MoveLine3; #Paste the third line
}
print $_; #Otherwise print the other lines
}

I'm sure there is an easier but thank you all the same for your input


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top