[Prev][Next][Index][Thread]

Re: a sed question... --> Perl




Use perl 
1. $ perl -p -e "s/foo/bar/;" filenames
It will replace inline in all files string foo by string bar
2. $ perl -p -i.bak -e "s/foo/bar/;" filenames
The same but will keep the orig files with .bak extension

or if you want a script:
1. 
#!/usr/bin/perl -p
s/foo/bar/;

2. 
#!/usr/bin/perl -pi.bak
s/foo/bar/;


Now for the second part:
-----------------------------------
#!/usr/bin/perl 

# Undefine line delimeter so we can read all the file in one var
undef $/;

# Define your old pattern
$old='<hr size="2">
<center>
<h4>Further information on the home page:</h4>
<h5><a href="huard.htm">[ Home </a>
<a href="news.htm">| News </a>
<a href="about.htm">| About </a>
<a href="resr.htm">| Funding </a>
<a href="mesg.htm">| Messages </a>
<a href="scol.htm">| Scholarships & Exchang Progs </a>
<a href="ftp.htm">| FTP ]</a></h5>';

# Define your new pattern
$new='<!--#include virtual="/bottom.raw" -->';
  
# Run for all filenames in ARGV
foreach $filename (@ARGV) {
  
  # Learn the mode of the file to restore it later
  $mode=(stat $filename)[2];   

  # Open the file and read it into $file
  open(FILE,$filename);  
  $file=<FILE>;
  
  # Make the substitution
  $file=~s/$old/$new/;
  close FILE;

  # Reopen the file for writing
  open(FILE,">$filename");

  # Print a new content into the same filename
  print FILE $file;
  
  # Restore the mode of the file
  chmod $mode, $filename;
}

 
> ok, now for a trick question... all the html files on this site have the
> same bottom block of 10 lines or so of links to various points in the
> tree, and I want to remove that block and stick a single line for SSI
> 
> in simple words: something like a block with
> 
> <hr size="2">
> <center>
> <h4>Further information on the home page:</h4>
> <h5><a href="huard.htm">[ Home </a>
> <a href="news.htm">| News </a>
> <a href="about.htm">| About </a>
> <a href="resr.htm">| Funding </a>
> <a href="mesg.htm">| Messages </a>
> <a href="scol.htm">| Scholarships & Exchang Progs </a>
> <a href="ftp.htm">| FTP ]</a></h5>
> 
> all exchanged for a single line saying:
> <!--#include virtual="/bottom.raw" -->
> 


-------------------------------------------------------------------------------
Stas Bekman 	sbekman@iil.intel.com.il  [ WebMaster at Intel Corp. ]
Address:	Aba Silver 12/29, Haifa, 32694, Israel
Phones:		[w]  972-(0)4-865-5188, [h]  972-(0)4-828-2264/3020 
Home Page:	http://techunix.technion.ac.il/~ofelix
Linux Page: 	http://www.mardanit.co.il/stas/linux.html
Linux-il Home:	http://www.linux.org.il/ 	


References: