Understanding regular expression usage in sed

While using sed, regular expressions are enclosed in forward slashes, as grep and sed use regular expressions and meta-characters for searching patterns in the file. An example of this would be the following:

    sed -n '/Regular_Expression/p' filename
    sed -n '/Mango/p' filename

This will print lines matching the Mango pattern:

    sed -n 's/RE/replacement string/' filename
    sed -n 's/Mango/Apple/p' filename

This will find the line containing the Mango pattern and then the Mango pattern will be replaced by the Apple text. This modified line will be shown on screen and the original file will be unchanged.

The following is a summary of various meta-characters and their usage in sed:

Meta-character ...

Get Learning Linux Shell Scripting - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.