linux 에서 sed 사용할 때 `(single quta) 어떻게 escspe 시키나요?

trymp의 이미지


linux 에서 sed 로 문서파일을 편집/치환할 수 있잖아요.

그런데 가령 'hellow' hellow -> 'love' heelow 로 치환 할때

sed -e 's/'\'hellow\'/\'love\'/g' -i data.txt

로 하면 애러가 나던데 중간에 '(single quta) 를 escape 처리할려면 어떻게 해야 하나요?

알려주시면 감사하겠습니다.

raymundo의 이미지

전체를 작은따옴표 대신 큰따옴표로 묶으세요.

$ cat data.txt
'hellow' hellow
$ sed -e "s/'hellow'/'love'/g" -i data.txt
$ cat data.txt
'love' hellow

스크립트 부분에 큰따옴표 작은따옴표 둘 다 들어갈 때가 제일 헷갈리는데, 저도 늘 끙끙대가 지금 찾아보니 \x27 이라고 써도 되나보네요.
https://stackoverflow.com/a/24509931/1150133

좋은 하루 되세요!

ymir의 이미지

sed 에 들어가는 스크립트 앞에 $ 를 넣어줘도 됩니다.

$ echo "'hellow' hellow" | sed $'s/\'hellow\'/\'love\'/g'
'love' hellow

되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』

chanik의 이미지

$를 작은따옴표 앞에 붙이는 것이 어떤 의미인지 몰라서 찾아봤습니다.
ANSI-C Quoting 이라고 부르는군요.
( https://www.gnu.org/software/bash/manual/html_node/ANSI_002dC-Quoting.html#ANSI_002dC-Quoting )

Quote:
ANSI-C Quoting

Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard

잘 배워 갑니다.
저처럼 몰랐던 분들께 참고가 될 것 같아 덧글 달아둡니다.

익명 사용자의 이미지

음...

이경우엔 그냥 쌍따옴표 써도 되는군요.

$ cat data.txt
'hellow' hellow
 
$ sed "s/'hellow'/'love'/g" < data.txt
'love' hellow