Is this possible? I read somewhere that the following command would do it:

sed -e [command] [file]

but it appeared to do the same thing as just

sed [command] [file]

(it did not save the changes). Is there any way to do this using sed?

  • 3
    The -e flag stands for expression. You probably want the -i flag which means in-place and I strongly recommend you to use sed -i.bak 's/../' filename Oct 13, 2013 at 19:39

3 Answers 3


I think you are looking for -i:

   -i[SUFFIX], --in-place[=SUFFIX]

          edit files in place (makes backup if SUFFIX supplied)

For example:

$ cat foo.txt
hello world
$ sed -i 's/o/X/g' foo.txt
$ cat foo.txt
hellX wXrld

If you provide a suffix, it will create a backup file:

$ ls
$ sed -i.bak 's/o/X/g' foo.txt 
$ ls
foo.txt  foo.txt.bak

The input file is modified and a backup containing the original file data is created.

Also note that this is for GNU sed, there are slight differences in format between different sed implementations.

  • 4
    Note that the man page is wrong. sed does not do in-place editing. It creates a new file and overwrites the old one, the inode changes.
    – Marco
    Oct 13, 2013 at 19:45
  • 2
    @Marco That's the safe way of editing a file in place. “Editing a file in place” in the sed documentation means that the original file (as defined by its name) is modified, not that the original file (as defined by its inode) is modified. Oct 13, 2013 at 22:13
  • 2
    @Gilles I totally agree that this is the preferred way to edit a file, but I'd hardly call it in-place. But that boils down to the question of how a file is identified, by inode or by name. I would say by inode, otherwise contructs like { rm file; cmd > file; } < file would lead to the conclusion that cmd reads and writes to the same file, which it does not. It's two distince files which just happen to have the same name. But I don't want to start a discussion here.
    – Marco
    Oct 13, 2013 at 22:38
  • On macOS 10.14 I get sed: 1: "foo.txt": invalid command code f when running sed -i 's/o/X/g' foo.txt. However, providing an extension, like sed -i .bak 's/o/X/g' foo.txt works fine. Any ideas how to get it working without creating backup files? Nov 29, 2018 at 19:53
  • 1
    Okay, figured it out. In macOS you need to explicitly add an empty string for the extension param option: sed -i '' 's/o/X/g' foo.txt Nov 29, 2018 at 19:57

These solution works for HPUX (UNIX):

1. { rm test1.sh && awk '{gsub("Error", "NO_Error", $0); print}' > test1.sh; } < test1.sh:

2. perl -pi -e 's/Error/NO_Error/g' test1.sh

3. sed 's/Error/NO_Error/g' test1.sh | tee test1.sh


-e option is for executing multiple sed commands

sed -e 's/linux/unix/' -e 's/os/OS/' file.txt

consider file.txt as

linux os

then O/P is

unix os

-i option saves changes permanently...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.