sed Cheatsheet - Command Line Text Replacement & Manipulation

Master sed commands for efficient text replacement, content manipulation, and blank line removal in files. A comprehensive cheatsheet for developers.

sed Cheatsheet

sed: Stream Editor for Text Manipulation

The `sed` command is a powerful stream editor for filtering and transforming text. It's commonly used in shell scripting for tasks like find and replace, deletion, and insertion within files. This cheatsheet covers essential `sed` operations for developers.

Replace Content with sed

A primary use case for `sed` is replacing specific patterns within a file. Let's illustrate with an example. Suppose you have a configuration file, `.env`, with the following content:

$ cat .env
DB_USER=admin
DB_PASS=__DB_PASS__

To substitute the placeholder __DB_PASS__ with a new value, such as 'secret', you would use the following `sed` command:

$ sed 's/__DB_PASS__/secret/g' .env

The s command signifies substitution, __DB_PASS__ is the pattern to find, secret is the replacement string, and g is a flag indicating a global replacement (all occurrences on a line). By default, `sed` outputs the modified content to standard output without altering the original file. To make the changes permanent, use the -i option:

$ sed -i 's/__DB_PASS__/secret/g' .env

If the replacement string is stored in an environment variable, like MYPASSWORD=secret, you can incorporate it directly into the `sed` command using double quotes:

$ MYPASSWORD=secret
$ sed -i "s/__DB_PASS__/${MYPASSWORD}/g" .env

Note for macOS users: On macOS, the -i option requires an argument for backup extension. To perform an in-place edit without creating a backup, use -i '':

$ sed -i '' "s/__DB_PASS__/${MYPASSWORD}/g" .env

Remove Blank Lines

Another common task is cleaning up files by removing empty or blank lines. Consider a configuration file, file.cfg, with extraneous blank lines:

$ cat file.cfg
[defaults]
key1=val1
key2=val2

key3=val3

To remove all blank lines from this file in-place, use the following `sed` command:

$ sed -i '/^$/d' file.cfg

Here, /^$/ is a regular expression that matches lines containing only the start-of-line anchor (^) followed immediately by the end-of-line anchor ($), effectively matching empty lines. The d command then deletes these matched lines.

Further Resources