sed problem. What am I doing wrong?
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]
精彩评论