Regex Find/Replace in Terminal

Using Regex to find/replace text in the terminal using sed

Regex Replace

Replacing regex strings is generally done with sed due to how powerful and simple to use it is.

One important limitation of using sed is that it only works with single-line regex patterns. Meaning that it cannot handle replacing multiple lines at once.

The -E flag tells sed to use full regex capabilities. It is also possible to use -e for some simpler operations, but it doesn't handle as many operators.

In Line

sed can be used to replace content in an input string in-line with other functions.

For example:

echo "my-string" | sed -E 's/my-.*/my-something/g'

# outputs: my-something

In Files

To replace regex text in a file, the following command can be used:

sed -i -E 's/id:.*/id: new-id/g' <file path>

Extra: Use Output from other Commands

Sometimes we want the replacement (or the search) string to be the output of another command. The commands above will support that, but you need to change the quotation marks used around the main sed parameter.

This is a bash-thing. Using double-quotes (") will allow variables and functions to be in-lined. Single-quotes (') will not interpret any special characters.

For example:

echo "my-string" | sed -E "s/my-.*/my-$RANDOM/g"

# outputs: my-1273

Extra: Re-using Matches

Another common use-case is to pull parts out of a string using a regex pattern and re-arranging, or cutting out certain parts. This can be done by referencing matches in the replacement string.

sed also supports this using the \<number> syntax. For example:

echo "my-string" | sed -E "s/my-(.*)/my-$RANDOM-\1/g"

# outputs: my-192837-something