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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.