How do I run the sed command with input and output as the same file?

sed -i (or the extended version, --in-place) will automate the process normally done with less advanced implementations, that of sending output to temporary file, then renaming that back to the original.

The -i is for in-place editing, and you can also provide a backup suffix for keeping a copy of the original:

sed -i.bak fileToChange
sed --in-place=.bak fileToChange

Both of those will keep the original file in fileToChange.bak.

Keep in mind that in-place editing may not be available in all sed implementations but it is in GNU sed which should be available on all variants of Linux, as per your tags.

If you’re using a more primitive implementation, you can use something like:

cp oldfile oldfile.bak && sed 'whatever' oldfile >newfile && mv newfile oldfile

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)