Skip to Content
Learning Linux Shell Scripting - Second Edition
book

Learning Linux Shell Scripting - Second Edition

by Ganesh Sanjiv Naik
May 2018
Beginner
332 pages
7h 28m
English
Packt Publishing
Content preview from Learning Linux Shell Scripting - Second Edition

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning Linux Shell Scripting

Learning Linux Shell Scripting

Ganesh Sanjiv Naik
Linux Shell Scripting Cookbook - Third Edition

Linux Shell Scripting Cookbook - Third Edition

Clif Flynt, Sarath Lakshman, Shantanu Tushar

Publisher Resources

ISBN: 9781788993197Supplemental Content