Sed - An Introduction and Tutorial by Bruce Barnett

submited by
Style Pass
2022-01-13 04:00:08

Last modified: Mon Dec 7 10:13:59 2020 Copyright 1994, 1995 Bruce Barnett and General Electric Company Copyright 2001,2005,2007,2011,2013 Bruce Barnett All rights reserved You are allowed to print copies of this tutorial for your personal use, and link to this page, but you are not allowed to make electronic copies, or redistribute this tutorial in any form without permission. Original version written in 1994 and published in the Sun Observer You can buy me a coffee, please

As a convenience, and to make my site more mobile-friendly, I moved my quick links to a new page: Click Here Table of Contents

Then click on the section header of any section to jump back to the table of contents. The Awful Truth about sed The essential command: s for substitution The slash as a delimiter Using & as the matched string Using \1 to keep part of the pattern Extended Regular Expressions Sed Pattern Flags /g - Global replacement Is sed recursive? /1, /2, etc. Specifying which occurrence /p - print Write to a file with /w filename /I - Ignore Case Combining substitution flags Arguments and invocation of sed Multiple commands with -e command Filenames on the command line sed -n: no printing Using 'sed /pattern/' Using 'sed -n /pattern/p' to duplicate the function of grep sed -f scriptname sed in shell scripts Quoting multiple sed lines in the C shell Quoting multiple sed lines in the Bourne shell sed -V sed -h A sed interpreter script Sed Comments Passing arguments into a sed script Using sed in a shell here-is document Multiple commands and order of execution Addresses and Ranges of Text Restricting to a line number Patterns Ranges by line number Ranges by patterns Delete with d Printing with p Reversing the restriction with ! Relationships between d, p, and ! The q or quit command Grouping with { and } Operating in a pattern range except for the patterns Writing a file with the 'w' command Reading in a file with the 'r' command The # Comment Command Adding, Changing, Inserting new lines Append a line with 'a' Insert a line with 'i' Change a line with 'c' Leading tabs and spaces in a sed script Adding more than one line Adding lines and the pattern space Address ranges and the above commands Multi-Line Patterns Print line number with = Transform with y Displaying control characters with a l Working with Multiple Lines Matching three lines with sed Matching patterns that span multiple lines Using newlines in sed scripts The Hold Buffer Exchange with x Example of Context Grep Hold with h or H Keeping more than one line in the hold buffer Get with g or G Branch (Flow Control) Testing with t Debugging with l An alternate way of adding comments The poorly documented ; Passing regular expressions as arguments Inserting binary characters GNU sed Command Line arguments The -posix argument The --version argument The -h Help argument The -l Line Length Argument The -s Separate argument The -i in-place argument The --follow-symlinks argument The -b Binary argument The -r Extended Regular Expression argument The -u Unbuffered argument The -z Null Data argument FreeBSD Extensions -a or delayed open The -I in-place argument -E or Extended Regular Expressions Using word boundaries Command Summary In Conclusion More References Introduction to Sed

How to use sed, a special editor for modifying files automatically. If you want to write a program to make changes in a file, sed is the tool to use.

Leave a Comment