Sed

Table of Contents

What is sed?

Sed is a “stream editor” for filtering and transforming text. In short, it’s an editor for modifying files automatically. So if you want to write a script that makes changes in a file, sed is the tool that you probably want to use for that.

s for substitution

While sed has a ton of commands, most people only learn the ’s’ command. And it’s understandable why that’s the case. The ’s’ command (substitution) is so useful. Let me show an example:

Replace the first occurrence of a regular expression in each line of a file:

  • sed s/find/replace/ <oldfile >newfile
  • sed s/red/green/ <.Xresources >sed-test
  • sed s/red/green/ .Xresources >sed-test (also works)
  • echo “Derek” | sed ’s/Derek/DT/’
  • echo “The Emacs file manager is dired” | sed ’s/red/green/’

Replace all occurrences of a regular expression in a file:

  • sed ’s/regularexpression/replacement/g’ filename
  • man sed | sed ’s/sed/SED/g’ | less
  • man sed | sed ’s/ sed /SED/g’ | less

Replace all occurrences of a string in a file, overwriting the file (i.e. in-place):

  • sed -i ’s/find/replace/g’ filename
  • sed -i ’s/Taylor/Tyler/g’ .bashrc
  • sed -i ’s/Tyler/Taylor/g’ .bashrc

Replace only on lines matching the line pattern:

  • tldr sed | sed ’/Replace/s/the/THE /’

Delete lines matching the line pattern:

  • tldr sed | sed ’/line_pattern/d’

Apply multiple find-replace expressions to a file:

  • sed -e ’s/find/replace/’ -e ’s/find/replace/’ filename
  • cat /etc/shells | sed -e ’s/usr/u/g’ -e ’s/bin/b/g’

Replace separator `/` by any other character not used in the find or replace patterns, e.g., `#`:

  • sed ’s#find#replace#’ filename
  • cat /etc/shells | sed -e ’s|usr|u|g’ -e ’s#bin#b#g’

Print only the lines containing the search string

  • cat /etc/shells | sed -n ’/usr/p’

PRO TIPS: Delete all spaces tabs at end of every line

Spaces

sed -i 's/ *$//' test.sh

Tabs

sed -i 's/[[:space:]]*$//' test.sh

Delete empty lines

  • cat test.sh | sed ’/^$/d’

Change lowercase to uppercase and vice versa:

  • sed ’s/[a-z]/\U&/g’ test.sh
  • sed ’s/[A-Z]/\L&/g’ test.sh

Print the first 11 lines of a file:

  • sed 11q filename (head replacement)
  • awk ’NR < 12’ (NR is # of lines seen so far)
  • head -11

Footer

Copyright © 2020-2021 Derek Taylor (DistroTube)

This page is licensed under a Creative Commons Attribution-NoDerivatives 4.0 International License (CC-BY-ND 4.0).

The source code for distro.tube can be found on GitLab. User-submitted contributions to the site are welcome, as long as the contributor agrees to license their submission with the CC-BY-ND 4.0 license.

Author: dt

Created: 2022-02-20 Sun 10:16