Hello LanX,
I missed the semicolon because of the question’s formatting, but I don’t think you need a look-behind to handle it; a simple look-ahead seems to be enough:
21:08 >perl -wE "my $x = '<Remarks>1 SW PLT SLAC 6 PCS </Remarks>'
+; $x =~ s/&#\d(?!\d);?//g; say $x;"
<Remarks>1 SW PLT SLAC 6 PCS </Remarks>
21:08 >perl -wE "my $x = '<Remarks>1 SW PLT SLAC 6 PCS &</Remarks>
+'; $x =~ s/&#\d(?!\d);?//g; say $x;"
<Remarks>1 SW PLT SLAC 6 PCS &</Remarks>
21:09 >perl -wE "my $x = '<Remarks>1 SW PLT SLAC 6 PCS </Remarks>';
+ $x =~ s/&#\d(?!\d);?//g; say $x;"
<Remarks>1 SW PLT SLAC 6 PCS </Remarks>
21:09 >
Or am I still missing something?
| [reply] [d/l] |
| [reply] |
This looks perfect. But I need to remove the entities without opening the file.
Using the unix sed command would be great...
Any other suggestion to replace the string without opening the file would be great...
| [reply] |
This looks perfect.
Please note that Athanasius's s/&#\d(?!\d);?//g solution also replaces  (including the semicolon at the end) with the empty string (see first code example) and does not replace & (no trailing semicolon) (update: or &# or & either, with or without trailing semicolon).
This does not square with my understanding of your OP, which led me to think that '&;' '&#;' '' should all be reduced to ';' (a lone semicolon). (tybalt89's solution exactly implements my understanding of the OPed requirements.)
... replace the string without opening the file ...
I have to say that I share Laurent_R's lack of understanding of what you mean by "without opening the file". How do you imagine that any operation, even simply reading the file, can be performed (update: on the contents of the file) without first opening the file? (Update: Although you could, of course, entirely delete the file and its contents without ever opening it.) Do you mean that you want code that is written without any explicit open function call? If so, what is the purpose of this requirement? Is this really just some oddball homework assignment?
Give a man a fish: <%-{-{-{-<
| [reply] [d/l] [select] |
Hi Chris Daniel,
I need to remove the entities without opening the file.
I'm going to take a guess as to what you mean by this. I assume you have an existing Perl script that uses an XML module to parse the file, and that is what you mean by "opening the file", and you want to make the replacements in the file before running this particular Perl script. The reason the terminology you're using is confusing is that a search & replace on the file will always require opening it.
It is possible but not necessary to use sed for your task, Perl will work just fine, this is a Perl website after all. You can either use a solution like for example what FreeBeerReekingMonk showed here, running that one-liner before you run your script that parses the XML file, or you could even integrate the search and replace into the same script that parses the XML file. Here's a rough idea that takes an input file, runs the search and replace and writes the result to an intermediate file, which you can then open with your XML parser:
my $input_filename = 'foo.xml';
my $temp_filename = 'bar.xml';
open my $ofh, '>', $temp_filename or die $!;
open my $ifh, '<', $input_filename or die $!;
while (<$ifh>) {
s/foo/bar/g;
print $ofh $_;
}
close $ifh;
close $ofh;
# now open $temp_filename with your XML parser
Hope this helps, -- Hauke D | [reply] [d/l] |
See what I posted above about the sed command, which is probably not supporting look-ahead assertions..
What do you mean by "without opening the file"? The sed command does open any file it works on, even it this is somewhat hidden from the user. And a Perl one-liner with the -pi.bak -e command-line options would just do the same as sed, hiding the opening file mechanism and doing an in-place replacement.
| [reply] [d/l] [select] |