I am trying to replace a word irrespective of it's case in a file using sed command as below:

sed -ie s/$tbl/$third_ele/gI $sql_file

where $tbl contains word S_ASSET and $third_ele contains table_01. This breaks when the file contains word S_ASSET_XA, the script replaces S_ASSET_XA to table_01_XA which i don't want. I need to match it exactly while replacing because i have another instance where S_ASSET_XA should be replaced with another word table_02.

How can i achieve this?

  • 2
    have you tried the \b word boundary after $tbl? – ruud Jun 4 '20 at 7:52
  • @ruud By introducing \b, none of the words are being replaced. I tried this sed -ie s/$tbl\b/$third_ele/gI $sql_file and sed -ie s/"$tbl"\b/$third_ele/gI $sql_file – Mohammed Arif Jun 4 '20 at 9:49

Use \b to mark a word boundary. Since you run from the command line, the escape character has to be doubled, since the shell does its own interpolation before handing it to sed:

sed -ie s/$tbl\\b/$third_ele/gI $sql_file

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.