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...