Home > OS >  sed command is throwing error : char 80: unknown command: `*'
sed command is throwing error : char 80: unknown command: `*'

Time:04-13

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 the sed s separator.
  • Any character sed uses as delimiter or operator must be backslashed to let sed know to treat it as text. Here, ., * have to.
  • Reversely, backslash the parenthesis so sed will know they are for grouping, not matching.
  • ^: starts with...
  • $: ends with...
  • \1 and \2 are used to build the result string. \1 is the content of the first parenthesis (com\.rs\.TestScopeService\.resourceScopes), \2 the second (\./\*\*={TestAPI}).
  • Related