Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To add an optional character to a regular expression using sed, you can use the question mark symbol, which means "zero or one occurrence of the preceding element". Here's an example:

Let's say you have a regular expression that matches the string "hello", but you want to make the letter "o" optional:

sed -e 's/hell[o]*/hello/g' input.txt

The square brackets around the "o" indicate that it's an optional character, and the asterisk after it means "zero or more occurrences". So this regular expression will match "hello" or "hell".