본문 바로가기
Linux/Shell

[macOS] sed -i 에러

by Sondho 2021. 2. 24.

sed: 1: "/etc/hosts": extra characters at the end of d command 발생

macOS에서 sed -i를 사용하여 파일안에 있는 문자를 치환하려 하는 경우, 아래와 같이 하면 작동하지 않는다.

sed 's/foo/bar/g' file
  • 해결 방법

      # BSD/macOS sed
      sed -i '' 's/foo/bar/'
    
      # GNU sed (대부분의 Linux)
      sed -i 's/foo/bar/g'
  • BSD/macOS sed가 정상적으로 작동하지 않는 케이스들

      **sed -i    's/foo/bar/' file  # Breaks; script is misinterpreted as backup-file suffix
      sed -i''  's/foo/bar/' file  # Ditto
      sed -i -e 's/foo/bar/' file  # -e is misinterpreted as backup-file suffix**

참고

https://stackoverflow.com/questions/16745988/sed-command-with-i-option-in-place-editing-works-fine-on-ubuntu-but-not-mac

 

sed command with -i option (in-place editing) works fine on Ubuntu but not Mac

I know nothing about Sed but need this command (which works fine on Ubuntu) to work on a Mac OSX: sed -i "/ $domain .*#drupalpro/d" /etc/hosts I'm getting: sed: 1: "/etc/hosts": extra characters...

stackoverflow.com

 

댓글