I am trying to replace string in property file using sed command.
Property file has below entry
com.rs.TestScopeService.resourceScopesA./**={TestAPI}
I want to replace this entry with below string
#com.rs.TestScopeService.resourceScopesB./**={TestAPI}
my sed command is --
sed -i '/com.rs.TestScopeService.resourceScopesA./**={TestAPI}/c\#com.rs.TestScopeService.resourceScopesB./**={TestAPI}' /Users/Desktop/test.properties
Above cmd throws error : sed: -e expression #1, char 80: unknown command: `*'.
Please help me in correcting the command to avoid error. Thanks!
CodePudding user response:
You can go around the * issues by adding a backshash before each *. Do this:
#!/bin/bash
originalstring="com.rs.TestScopeService.resourceScopesA./**={TestAPI}"
echo "$originalstring" | sed 's%^\(com\.rs\.TestScopeService\.resourceScopes\)A\(\./\*\*={TestAPI}\)$%#\1B\2%'
- I used character
%as thesedsseparator. - Any character
seduses as delimiter or operator must be backslashed to letsedknow to treat it as text. Here,.,*have to. - Reversely, backslash the parenthesis so
sedwill know they are for grouping, not matching. ^: starts with...$: ends with...\1and\2are used to build the result string.\1is the content of the first parenthesis (com\.rs\.TestScopeService\.resourceScopes),\2the second (\./\*\*={TestAPI}).
