I have the following block of text (with \r\n or \n) and I would like to find and delete it with sed.
<?php
/*
*/
?>
I've tried many embarrassing things (based on many SE answers) to remove this which have failed miserably so rather than muddy the waters what is the right way to capture and delete this pattern? Using two separate sed commands for either \n or \r\n is fine too.
Ok, I'll share two poor attempts:
sed 'N;s/<\?php\r\n\/\*\r\n\*\/\r\n\?>//g' file.txt
sed ':a;N;$!ba;s/<\?php\r\n\/\*\r\n\*\/\r\n\?>//g' file.txt
EDIT: Based on the answer below I tried to put this into a PERL recursion routine that searchers for .php files and modifies them. However $text ends up undefined. The error is "Use of uninitialized value $text in print at [line "print $text"]"
Sorry, I've not used perl before...
#!/usr/bin/perl
use strict;
use warnings;
my $parent_dir = ".";
my $dir="";
my $file="";
process_dir($parent_dir);
sub process_dir {
my $dir = shift;
print "Processing $dir\n";
opendir(my $SCR , $dir) or die "Can't open $dir: $!";
while( defined (my $file = readdir $SCR) )
{
next if ($file =~ /\.$/ );
if ( $file =~ /\.php$/ ) {
&process_file();
} elsif ( -d "$dir/$file" ) {
print "directory : $dir/$file\n";
process_dir("$dir/$file/");
#next;
#} elsif ( $file
} else {
print "Else :$file\n" if ( -B "$dir/$file");
}
print "file -> $file\n";
}
closedir($SCR);
}
sub process_file{
my $text="";
open(my $fh, '<', "$dir/$file") or die "cannot open file $file";
{
local $/;
$text = <$fh>;
}
close($fh);
print "Before:\n";
print $text;
$text =~ s{ <\?php \s* \r?\n \s* /\* \s* \r?\n \s* \*/ \s* \r?\n \?> \s* \r?\n }{}gmx;
print "After:\n";
print $text;
}