开发者

sed problem. What am I doing wrong?

开发者 https://www.devze.com 2023-03-05 16:42 出处:网络
I am trying to repla开发者_如何学Cce a[ \'xxx\' ] by a[ xxx ] using sed: sed -e \'s/a[ \'\\(.*\\)\' ]/a[ \\1 ]/\' ./Test

I am trying to repla开发者_如何学Cce a[ 'xxx' ] by a[ xxx ] using sed:

sed -e 's/a[ '\(.*\)' ]/a[ \1 ]/' ./Test
sed: -e expression #1, char xx: invalid reference \1 on `s' command's RHS

What am I doing wrong?

Thanks!


You need to escape the [ and ] like this:

echo "a[ 'xxx' ]" | sed "s/a\[ '\(.*\)' \]/a[ \1 ]/"


How about simply deleting the single-quote characters? To avoid confusing the shell, use double-quotes around the expression:

 sed -e "s/'//g"

a['xxx']

a[xxx]

Greg Johnson


What about using tr to delete the single quotes?

printf "a['xxx']\n" | tr -d "'"
a[xxx]
0

精彩评论

暂无评论...
验证码 换一张
取 消