How to Use sed Command

The sed command is primarily used to replace text in a file with a regular expression, but it can also be used to search and delete text.

For example, there is a php.ini file. Need to replace memory_limit = 128M with memory_limit = 256M.

sed command in this case would look like this:

sed 's/memory_limit.*$/memory_limit = 256M/' php.ini | grep memory_limit
memory_limit = 256M

The command above is used as a regular expression check, the changes will not be written to the file, but only the replaced text that is filtered out by the grep command will be displayed.

To write to a file, need to add the -i option at the beginning:

sed -i 's/memory_limit.*$/memory_limit = 256M/' php.ini