Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed edit file in place

Tags:

unix

sed

solaris

I am trying to find out if it is possible to edit a file in a single sed command without manually streaming the edited content into a new file and then renaming the new file to the original file name. I tried the -i option but my Solaris system said that -i is an illegal option. Is there a different way?

like image 947
amphibient Avatar asked Oct 02 '12 18:10

amphibient


People also ask

Does sed edit file?

There is a huge pool of commands available for Ubuntu and sed command utility is one of them; the sed command can be used to perform fundamental operations on text files like editing, deleting text inside a file.

How do you replace a word in all files in a directory Linux?

s/search/replace/g — this is the substitution command. The s stands for substitute (i.e. replace), the g instructs the command to replace all occurrences.

Does sed use regex?

Regular expressions are used by several different Unix commands, including ed, sed, awk, grep, and to a more limited extent, vi.


1 Answers

The -i option streams the edited content into a new file and then renames it behind the scenes, anyway.

Example:

sed -i 's/STRING_TO_REPLACE/STRING_TO_REPLACE_IT/g' filename 

while on macOS you need:

sed -i '' 's/STRING_TO_REPLACE/STRING_TO_REPLACE_IT/g' filename 
like image 120
choroba Avatar answered Sep 22 '22 14:09

choroba