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

Performing replacement directly in the file

When a filename is passed to sed, it usually prints to stdout. The -I option will cause sed to modify the contents of the file in place:

    $ sed 's/PATTERN/replacement/' -i filename

For example, replace all three-digit numbers with another specified number in a file, as follows:

    $ cat sed_data.txt
    11 abc 111 this 9 file contains 111 11 88 numbers 0000

    $ sed -i 's/\b[0-9]\{3\}\b/NUMBER/g' sed_data.txt
    $ cat sed_data.txt
    11 abc NUMBER this 9 file contains NUMBER 11 88 numbers 0000

The preceding one-liner replaces three-digit numbers only. \b[0-9]\{3\}\b is the regular expression used to match three-digit numbers. [0-9] is the range of digits from 0 to 9. The {3} string defines the count of digits. ...

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