In a file, I want to replace

COMMENT AS `<some_comment>`


COMMENT '<some_comment>'

That is, replace COMMENT AS with just COMMENT, and replace the backticks around the following comment - but without replacing backticks anywhere else. How would I do this, from the command line?


You could use sed, with careful quoting:

sed "s/^COMMENT AS \`\(.*\)\`$/COMMENT '\1'/"

Here I used double-quotes to surround the statement because of the internal single-quotes.

The back-ticks have to be escaped from the shell.

The parenthesis are escaped to make sed capture them (for later re-insertion with the \1 token).

I've also anchored the text to the beginning (^) and end ($) of line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.