find, replace and escape string linux
I'm trying find all instances of a string and replace them, the original string looks like this:
开发者_JS百科<li><a href="#" onclick="alert('soon!');">Some Text Here</a></li>
the replacement looks like this:
<li><a href="<?php print $somevar;?>/some.php">Something new</a></li>
What would be a good way to do this in the CLI
Thanks
I think the sed command would do the job nicely, provided your onclick handler and the "Some Text Here" don't include any nested HTML tags that the regex might confuse for the closing tags of the replacement string.
Searching and replacing in HTML is a guaranteed headache. At some point someone will pass you malformed HTML and even the most careful crafted regexp will fail horribly.
I'd definitely work with the HTML at the highest possible level of abstraction, preferably a homegrown tool that uses DOM or SAX.
For a quick fix
- A command line tool using XSL/XSLT
Standard way of searching and replacing in linux command line is sed, eg:
sed -i yourfilename -e "s%textyouwantoreplace%newtext%g"
The only thing is, sed uses regular expressions, so you'll need to escape stuff that might be a wildcard, by putting a \ before it, eg write \$ instead of $.
-i means: edit the file in-place
-e means: the next thing in the commandline is an expression to evaluate, ie the whole thing in quotes after it
's' means 'substitute'
'g' means 'global' substitution
精彩评论