Skip to Content
Linux Shell Scripting Cookbook - Third Edition
book

Linux Shell Scripting Cookbook - Third Edition

by Clif Flynt, Sarath Lakshman, Shantanu Tushar
May 2017
Beginner
552 pages
28h 47m
English
Packt Publishing
Content preview from Linux Shell Scripting Cookbook - Third Edition

Substring match notation (\1)

& corresponds to the matched string for the given pattern. Parenthesized portions of a regular expression can be matched with \#:

    $ echo this is digit 7 in a number | sed 's/digit \([0-9]\)/\1/'
    this is 7 in a number

The preceding command replaces digit 7 with 7. The substring matched is 7. \(pattern\) matches the substring. The pattern is enclosed in () and is escaped with backslashes. For the first substring match, the corresponding notation is \1, for the second, it is \2, and so on.

    $ echo seven EIGHT | sed 's/\([a-z]\+\) \([A-Z]\+\)/\2 \1/'
    EIGHT seven

([a-z]\+\) matches the first word and \([A-Z]\+\) matches the second word; \1 and \2 are used for referencing them. This type of referencing is called ...

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

Mastering Linux Shell Scripting - Second Edition

Mastering Linux Shell Scripting - Second Edition

Mokhtar Ebrahim, Andrew Mallett

Publisher Resources

ISBN: 9781785881985