The sed command in Linux: stream editor for transforming text

Introduction

Sed is an indispensable tool in the arsenal of any system administrator or developer working in Linux environments. Its name stands for ‘stream editor’ and its main function is to process text streams linearly, applying transformations without needing to load the entire file into memory. This makes it extremely efficient for large files or for use within pipes and scripts. In this article we will explore from basic syntax to advanced examples that will allow you to make the most of sed’s power.

What is sed?

Although sed may appear cryptic at first glance, its logic is simple: it reads input line by line, executes the instructions we give it, and sends the result to standard output. It does not modify the original file unless we explicitly tell it to with options like -i. This feature makes it a safe ally for testing and prototyping, as we can experiment without risk of damaging data.

Basic Syntax

The simplest way to invoke sed is: sed ‘command’ file. The command is placed in single quotes to prevent the shell from interpreting special characters. The most frequent commands begin with an address that specifies which lines to affect, followed by the action. For example, ‘5,10s/foo/bar/’ substitutes foo with bar only on lines 5 through 10. If the address is omitted, the action applies to all lines.

Most Common Operations

  • Substitution: the s/pattern/replacement/flags command changes the first occurrence of pattern on each line. Adding the g flag substitutes all occurrences.
  • Deletion: the d command deletes lines that match the given address. For example, ‘/^#/d’ removes all lines that begin with the hash symbol.
  • Insertion: with i\ text inserts text before the selected line, while a\ text appends it after.
  • Printing: the p command displays lines that meet a condition, useful when combined with the -n option to suppress automatic output.

Practical Examples

  • Changing the extension of several files in a listing: ls | sed ‘s/\.txt$/\.md/’
  • Removing comments from a shell script: sed ‘/^#/d’ script.sh > script_limpio.sh
  • Adding line numbers to a file: sed ‘=’ file | sed ‘N;s/\n/\t/’
  • Converting email addresses to lowercase: sed ‘s/.*/\L&/’ input.txt

Advanced Usage

Sed allows chaining multiple expressions using -e or simply separating them with a semicolon inside a single quote. For example, sed -e ‘s/foo/bar/’ -e ‘s/baz/qux/’ file applies both substitutions. It also provides two workspaces: pattern space and hold space. With the commands h, H, g, G, and x we can move data between them, enabling transformations that depend on previous or following lines, such as duplicating lines or creating summaries.

Another powerful feature is the ability to read command scripts from a file with -f script.sed, which facilitates reuse of complex routines across different projects.

Tips and Best Practices

  • Always test your commands with a backup or using standard output before applying -i.
  • Use single quotes around the sed script to prevent the shell from expanding variables or interpreting backslashes.
  • When you need to include a forward slash in the substitution pattern, consider using a different delimiter, for example, s|old/path|new/path|.
  • Take advantage of the -n option together with p to print only what you really need, reducing noise in the output.
  • Document your sed scripts with comments inside the .sed file, starting each line with #.

Conclusion

The sed command remains a key piece for anyone working with text in Linux. Its ability to process streams quickly, its flexible syntax, and its low resource consumption make it ideal for simple substitution tasks and for more elaborate transformation scripts. Mastering sed not only saves time, but also opens the door to efficient automation of processes that would otherwise require more complex programming. We invite you to practice the examples shown and to explore its manual to discover even more tricks that will make your daily work more productive.

This post is also available in ESPAÑOL.

Leave a Reply

Your email address will not be published. Required fields are marked *

Esta obra está bajo una Licencia Creative Commons Atribución 4.0 Internacional para Francesc Roig francesc@vivaldi.net .