More Practical Examples of Using sed on Debian 12

Building on the basics, it becomes clear that sed is more than just a simple search-and-replace tool. Its true power lies in how it handles streams of text with pattern-matching precision. On Debian 12, sed is often used in shell scripts or piped from other commands like grep or cat. In this article, we’ll look at additional examples to deepen your understanding of what sed can do.

Let’s begin with line-specific edits. If you want to change text only on a specific line number, you can prefix your command with the line number. For example, to replace the word “apple” with “orange” only on line 2:

sed '2s/apple/orange/' file.txt

This will leave other instances of “apple” on other lines unchanged. You can also apply changes to a range of lines:

sed '5,10s/foo/bar/g' file.txt

This substitutes all occurrences of “foo” with “bar” from lines 5 through 10.

Another useful feature is inserting or appending lines. To insert a line before a specific pattern, use the i command:

sed '/^Title:/i\Author: Unknown' file.txt

This adds the line “Author: Unknown” before every line that starts with “Title:”. Similarly, to append a line after a pattern, use a:

sed '/^Title:/a\Published: 2025' file.txt

Next, let’s look at removing or replacing multiple words using alternation. To replace “cat” or “dog” with “animal”:

sed 's/\b\(cat\|dog\)\b/animal/g' file.txt

Explanation:

  • \b is a word boundary anchor.
  • \(cat\|dog\) uses escaped parentheses to group options, and \| acts like OR.
  • g makes it global for each line.

Let’s say you want to swap the positions of two words. If a line contains “first second” and you want to change it to “second first”, use:

sed 's/^\([a-zA-Z]\+\) \([a-zA-Z]\+\)$/\2 \1/' file.txt

Breaking that down:

  • ^\([a-zA-Z]\+\) captures the first word.
  • \([a-zA-Z]\+\)$ captures the second word.
  • \2 \1 swaps their order in the replacement.
  • \+ matches one or more letters, and the brackets capture the groups.

For substitutions that include slashes, use a different delimiter to avoid escaping them. For instance, to replace a path like /home/user with /mnt/backup, use:

sed 's|/home/user|/mnt/backup|g' file.txt

Here, | is used instead of / to reduce clutter and avoid unnecessary backslashes.

You can also remove trailing whitespace from lines with:

sed 's/[ \t]*$//' file.txt

This matches any number of spaces or tabs at the end of a line ([ \t]*$) and replaces them with nothing.

For more dynamic changes, sed can use shell variables:

word="apple"
sed "s/$word/fruit/" file.txt

Double quotes allow the variable to expand, making the substitution flexible in scripts.

Through these examples, it’s clear that sed is not just useful for quick edits—it’s a scripting powerhouse for automating text transformations with surgical precision. When combined with other tools in the Unix philosophy, sed is an essential part of any Debian user’s toolkit.

AIrticles is a blog containing posts written 100% with AI. They haven’t been vetted by a human. The purpose of the site is to act as a personal resource which can be used for reading enjoyment (e.g., posts on topics that interest me), or as an aide in doing things (e.g., how-tos). Posting these articles online means I can access them anywhere. You are welcome to read them too, but just remember they are unvetted AI outputs, so they may not be accurate. By continuing to access this page you acknowledge this!

Comments

Leave a Reply