Sed, short for stream editor, is a powerful tool used on the command line for parsing and transforming text. It is particularly useful for batch-editing files, filtering text in pipelines, and making automated replacements. Debian 12 includes sed by default as part of its core utilities, making it readily available to any user comfortable working from a terminal.
At its core, sed processes input line by line, applying the commands given to it. The most common use case is substitution, which uses the s command. A basic syntax looks like this:
sed 's/old/new/' file.txt
This command replaces the first occurrence of “old” with “new” on each line of file.txt. Breaking it down:
's/old/new/': This is the substitution command.sstands for substitute./is the delimiter separating the pattern from the replacement.oldis the regular expression pattern to match.newis the string to insert in its place.
file.txtis the file being processed.
By default, sed prints the modified output to the terminal rather than modifying the file in place. To update the file directly, you can use the -i flag:
sed -i 's/old/new/' file.txt
This tells sed to edit the file in place. It’s often wise to make a backup first. You can do that with:
sed -i.bak 's/old/new/' file.txt
This creates a backup named file.txt.bak before modifying the original.
Now let’s consider an example using a regular expression. Suppose you want to replace any number with the word “number”:
sed 's/[0-9]\+/number/g' file.txt
Here’s what’s happening:
[0-9]matches any single digit.\+matches one or more of the preceding pattern. This is a common POSIX-style way of expressing repetition.- Together,
[0-9]\+matches any whole number. gat the end stands for global, meaning replace all matches on each line, not just the first.
If you want to delete lines that contain a certain pattern, use the d command:
sed '/^#/d' file.txt
This deletes all lines that start with a #—commonly used to strip out comments in configuration files.
/^#/: A regular expression that matches any line beginning with#.^anchors the match to the start of the line.
dtellssedto delete those matching lines.
Another helpful trick is printing only lines that match a pattern. For example:
sed -n '/error/p' file.txt
-nsuppresses automatic printing of lines./error/matches any line containing the word “error”.ptellssedto print lines matching the pattern.
With sed, you can chain multiple commands. For example, to replace tabs with spaces and delete empty lines:
sed -e 's/\t/ /g' -e '/^$/d' file.txt
-eallows multiple expressions.\tmatches a tab character.- is four spaces, used as the replacement.
/^$/ddeletes empty lines—^$matches lines with no content.
While sed can appear cryptic at first, mastering its basic syntax opens the door to efficient and powerful text processing, especially in shell scripts or when working with logs and configuration files. On Debian 12, it’s a tool that can save time and avoid repetitive manual edits.
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!

Leave a Reply
You must be logged in to post a comment.