A Developer's Guide to Sed: Mastering the Text Substitution Tool

2023/06/07
This article was written by an AI 🤖. The original article can be found here. If you want to learn more about how this works, check out our repo.

If you're a developer who works with text files, then you've probably heard of sed. Sed is a command-line tool that allows you to perform text substitution, just like the "Find and Replace" feature in GUI editors. While there are newer tools like sd that offer a better user experience, learning sed is still worthwhile for developers who want to have a deeper understanding of text processing.

At its core, sed is a non-interactive text editor that comes with a built-in Turing machine. This means that you can even run games like Tetris on it! However, mastering sed takes some time, and this guide aims to help you get started with the basics.

Invocation and Basic Syntax

The typical invocation of sed is sed SCRIPT FILE. For example, to replace all occurrences of "foo" with "bar" in a file called input.txt, you would use the following command:

sed 's/foo/bar/g' input.txt

The "s" in the command stands for "substitute", and the "g" at the end stands for "global", which means that sed will replace all occurrences of "foo" with "bar" in the file.

By default, sed prints all processed input, reflecting any modifications made. You can suppress this output with the -n/--quiet/--silent option. In that case, you need to run specific commands to get any output at all, like the "p" command:

sed -n 's/foo/bar/p' input.txt

Here, the "p" command tells sed to print only the lines that have been modified.

Commands and Addressing

A sed script consists of one or more commands that follow the basic syntax [addr]x[options]. The "addr" part specifies the lines to which the command applies, and the "x" part specifies the action to be performed. For example, the command:

3s/foo/bar/g

will search for the word "foo" on line 3 and replace it with "bar". You can also specify a range of lines by separating two addresses with a comma. For example, the command:

2,5s/foo/bar/g

will replace all occurrences of "foo" with "bar" on lines 2 through 5.

Here are some of the most commonly used sed commands:

  • "s": substitute
  • "d": delete
  • "p": print
  • "a": append
  • "i": insert

For example, the command:

sed '2,5d' input.txt

will delete lines 2 through 5 from the file.

Regular Expressions

One of the most powerful features of sed is its support for regular expressions. Regular expressions allow you to search for patterns in text, rather than just exact matches. Here are some examples of regular expressions that you can use with sed:

  • "." matches any character
  • "^" matches the beginning of a line
  • "$" matches the end of a line
  • "[abc]" matches any of the characters "a", "b", or "c"
  • "[a-z]" matches any lowercase letter
  • "[A-Z]" matches any uppercase letter
  • "[0-9]" matches any digit

For example, the command:

sed 's/^foo/bar/g' input.txt

will replace all occurrences of "foo" at the beginning of a line with "bar".

Conclusion

Sed is a powerful tool that can help you perform text substitution and processing tasks quickly and efficiently. While it may take some time to master, learning sed is a valuable skill for any developer who works with text files. In this guide, we covered the basics of sed, including its invocation, syntax, commands, addressing, and regular expressions. With this knowledge, you can start using sed to automate your text processing tasks and become a more efficient developer.