Once you’ve mastered the basics and a few advanced patterns, sed starts to feel less like a tool and more like a language. Its syntax may be terse, but it allows for elegant and powerful transformations. This article explores even more examples of how sed can be used effectively on Debian 12, especially for creative or specialized tasks.
Let’s begin with extracting specific parts of a line. Say you have a CSV file and want only the second column. Assuming comma-separated values:
sed 's/^[^,]*,\([^,]*\).*/\1/' file.csv
Here’s the breakdown:
^[^,]*,matches the first column and comma.[^,]*matches any character except commas.\([^,]*\)captures the second column (everything up to the next comma)..*matches the rest of the line.\1replaces the entire line with the second column.
This is useful when cut isn’t flexible enough—for example, if delimiters are inconsistent.
Another handy example is wrapping each line with quotes. If you want to convert:
apple
banana
cherry
into:
"apple",
"banana",
"cherry",
You can use:
sed 's/.*/"&",/' list.txt
.*matches the whole line."&”` adds quotes around the entire match.- The trailing comma is appended as part of the replacement.
Suppose you want to insert a line after every 3 lines—for example, adding a divider line. Use a counter with ~:
sed 'n;n;a---' file.txt
This prints the first line normally, skips the next two with n, and then appends ---. It then repeats. This works when line structure is predictable.
To replace the last word in a line:
sed 's/\s\+\([^ \t]*\)$/ [\1]/' file.txt
This finds the final whitespace-separated word and puts it in brackets.
\s\+matches one or more spaces or tabs.\([^ \t]*\)$captures the final non-whitespace sequence at the end of the line.- The replacement adds brackets around the last word.
Let’s say you want to number each line of a file:
sed = file.txt | sed 'N;s/\n/ /'
- The first
sed =prints line numbers followed by the lines. - The second
sedjoins each number with its corresponding line and replaces the newline with a space.
Sometimes you want to make complex changes but only after a pattern has been matched. For instance, changing the format of all lines after a certain marker:
sed '/^START$/,$s/foo/bar/g' file.txt
/^START$/,$limits the substitution range to all lines from the line that says “START” to the end.s/foo/bar/gapplies globally on those lines.
For working with JSON-like structures, you might want to collapse multiple lines into one:
sed ':a;N;$!ba;s/\n/ /g' file.json
:amarks a label.Nappends the next line.$!barepeats this process until the last line.- The
s/\n/ /greplaces all newlines with spaces.
These examples show that sed can serve as a mini text processor, capable of restructuring, sanitizing, and formatting content in countless ways. While sed has limitations (especially with deeply nested or hierarchical data), for linear and pattern-based tasks, it’s fast, scriptable, and available out-of-the-box on Debian 12.
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.