LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 03-02-2010, 04:45 AM   #1
Gelon
LQ Newbie
 
Registered: Mar 2010
Posts: 3

Rep: Reputation: 1
Question Replace html in multiple files (80+)


Dear Linux users

I'm not very well know with linux commands so hopefully you can help me out. I want to change some HTML in my files (80+ so I prefer not to do this by hand).

I got a script to replace a string in files, however the HTML I want to replace contains an enter (and tab of 4 spaces) (explanation below)

Does someone know how I could update my HTML by using a function in Linux?

I also found sed but don't know how to use it with the enter (linenumbers will not work because they variate)...

****

Source:
Code:
    </div>
    <!-- END YEAR MENU -->
Target:
Code:
    </td>
    <!-- END YEAR MENU -->
I could not replace all </div> because that will override some others

Friends code:
Code:
#!/usr/bin/perl -w
use strict;
use diagnostics;

if (scalar(@ARGV) < 3) {
    print "Syntax: $0 FROM TO <files>$/";
    exit(0);
}

my $from = shift @ARGV || "";
my $to = shift @ARGV || "";
my @files = @ARGV;

# check input
if (($from =~ /[\%|\$|\\]/) || ($to =~ /[\%|\$|\\]/)) {
    print "FROM and TO may not contain \%, \$ or \\ characters!$/";
    exit(0);
}

#$from =~ s/\$/\\\$/g;
#$to =~ s/\$/\\\$/g;

print "Replacing $from with $to...$/";

foreach my $file (@files) {
    if (open(INPUT, "<$file")) {      
	my @content = <INPUT>;
	my $result = ();
        my $changed = 0;
	foreach my $line (@content) {
	    if ($line =~ s/$from/$to/g) {
		$changed++;
	    };	    
	}
	if ($changed > 0) {
	    if (open(OUTPUT, ">$file")) {
		print "Changed $file $changed time(s).$/";
		print OUTPUT @content;
	    } else {
		print STDERR "Cannot write $file!$/";	    
	    }
	}
    } else {
	print STDERR "Cannot read $file!$/";
    }
}
Executable with: ./replace 'FROM' 'TO' files
Using a /n or /r did not work (even with removing the / check...

Last edited by Gelon; 03-02-2010 at 04:47 AM. Reason: Added: what I allready foud
 
Old 03-02-2010, 06:08 AM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
The following may help:
Code:
perl -p0777i -e 's:</div>(\s*<!-- END YEAR MENU -->):</td>$1:g' *.html
The '0777' flag changes the record separator. This means the whole file will get treated as a single line. The 'i' flag does the operation in place (replacing the existing file with the modified one).

Last edited by neonsignal; 03-02-2010 at 06:10 AM.
 
Old 03-02-2010, 06:43 AM   #3
Gelon
LQ Newbie
 
Registered: Mar 2010
Posts: 3

Original Poster
Rep: Reputation: 1
Thanks that worked just had to change <!-- to <\!--
 
  


Reply



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
Find & Replace string in multiple files Rudy Linux - General 14 04-15-2010 08:10 AM
Search and replace across multiple files zouriel Programming 11 12-07-2007 04:36 PM
Search and Replace over multiple files The_Nerd Linux - Software 8 06-20-2004 06:59 AM
How to replace text in multiple files bpk Linux - Newbie 2 02-10-2004 02:03 PM
how to print multiple html files at once? coontie Linux - Software 0 07-01-2003 10:56 AM

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

All times are GMT -5. The time now is 05:57 PM.

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