Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 38 ยท sed Scripting

sed Append, Insert, Change, y and l Commands

Add and replace whole lines with sed: the a, i and c commands, y for transliteration, l to reveal hidden characters, and r and w for reading and writing files.

Substitution edits text inside a line. Sometimes you want to add a whole new line after a match, put a header at the top, replace an entire line, or swap characters one for one. sed has a family of commands for these jobs: a, i, c, y, l, r, w. They are the first commands you will usually put in a script file.

a, i and c

  • a text: append a line after the selected line.
  • i text: insert a line before it.
  • c text: change (replace) the selected line, or the whole range.

In GNU sed the one-line form a text works. POSIX requires the text on the following line, after a backslash:

TEXT
/pattern/a\
the text to append

The portable form always works and also allows several lines of text (end each but the last with a backslash).

Bash Append, insert and change

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Multiple lines and leading blanks

To add more than one line, end each text line with a backslash. In the POSIX form, leading whitespace of the text is stripped by some seds; GNU sed keeps it in the portable form, and in the one-liner form you can protect it with a backslash before the first blank (a\ indented).

Bash Multi-line text and indentation

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Change on a range

c with a single address replaces each matching line. With a range, GNU sed (and POSIX) replaces the whole range with one copy of the text, printed when the range ends. This is a common surprise:

Bash c on a single line and on a range

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

If you want to replace every line of a range, wrap it in braces: 2,4{c\ ... } still gives one print in some seds; the safer way is 2,4s/.*/BLOCK/.

y: transliterate

y/abc/xyz/ replaces each character in the first list with the character at the same position in the second. The lists must be the same length. Unlike s, there are no regexes and no flags; it's a character translation like tr.

Bash Transliteration with y

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

y accepts \n (newline), \\ and an escaped delimiter in its lists, which is its main advantage over tr when working inside sed scripts.

l: show hidden characters

l prints the pattern space in an unambiguous form: tabs as \t, newlines as \n, other control characters as octal escapes, and $ at the end of the line. It is the best way to find stray tabs, trailing spaces and carriage returns. l 40 wraps the output at 40 columns.

Bash l reveals the invisible

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

You will see a\tb \r$: a tab, two trailing spaces and a Windows carriage return, none of which are visible normally.

r and w: reading and writing files

r file appends the contents of file after the selected line. w file writes the selected lines to a file. Special names in GNU sed: /dev/stdout and /dev/stderr.

Bash Inserting a file and splitting by pattern

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The r file is read when the cycle ends, so it appears after the current line even if later commands d that line. The w command truncates the file when sed starts, even if nothing is written. Be careful not to write to the file you are reading.

Summary of the line-oriented commands

Command Effect Example
a\text add after $a\THE END
i\text add before 1i\HEADER
c\text replace line(s) /old/c\new
y/abc/xyz/ transliterate y/ /_/
l print visibly sed -n l
r file add file after /x/r snippet
w file write matching lines /err/w e.txt
= line number $=
Bash A worked example: adding a license header and a footer

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

1i\ inserts before the first line, and $a\ appends after the last. It is the standard recipe for adding a banner to source files.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which command inserts a line BEFORE line 1?

    i (insert) places text before the selected line, a after it, and c replaces it.
  2. 2. What does sed '2,4c\X' print for the lines 2, 3 and 4?

    With a range, c deletes the lines and prints the text once at the end of the range.
  3. 3. What does sed 'y/-/_/' do?

    y translates every character in the first list to its partner in the second list, on the entire line.
  4. 4. Why use sed -n l?

    l shows invisible characters as escapes and marks line ends with $.

Summary

  • a, i, c add or replace whole lines; the portable form puts the text on the next line after a backslash.
  • c on a range prints its text once.
  • y transliterates characters; l makes hidden characters visible.
  • r reads a file into the output; w writes selected lines to a file.

Next: The Hold Space