As you continue to explore sed on Debian 12, you’ll find it remarkably capable of solving complex text manipulation tasks with concise commands. This follow-up provides deeper examples that demonstrate sed’s flexibility, including pattern-based logic, conditional editing, and multi-line operations.
Let’s begin with conditional deletion. Suppose you want to delete lines that contain “debug” unless they also contain “keep”. You can achieve this with:
sed '/debug/ { /keep/!d }' file.txt
This breaks down as follows:
/debug/identifies the lines to examine.{ /keep/!d }applies a command block:/keep/!is a negated match—meaning lines not containing “keep”.ddeletes those lines.
Next, to change all occurrences of multiple words to different values, sed can use sequential commands:
sed -e 's/blue/green/g' -e 's/cat/dog/g' file.txt
Each -e provides a separate editing rule. This is useful when transforming various words in one pass without chaining multiple sed invocations.
To remove duplicate lines while preserving order, you can use a trick with sed and its hold space:
sed -n '/./{H;g;/^\(.*\n\)\(.*\n\)*\2$/!p}' file.txt
This is a more complex example. While sed is not designed for deduplication, this hack works for small files. It stores lines in the hold buffer and only prints them if they haven’t already appeared. For robust deduplication, however, awk or sort -u is usually better.
For matching patterns across line boundaries, standard sed falls short, since it processes one line at a time. But GNU sed supports the N command to include the next line in pattern space. For example, to replace two consecutive lines that say:
Name: John
Age: 30
with a single line:
John (30)
you could use:
sed -n 'N;s/Name: \(.*\)\nAge: \(.*\)/\1 (\2)/p' file.txt
Here’s how it works:
-nsuppresses automatic printing.Nappends the next line to the pattern space.- The
scommand uses regex groups:\(.*\)captures the name and age.\nmatches the newline between lines.
\1 (\2)restructures the data.pprints the result.
You can also change the case of text using GNU sed with \L, \U, and \E. To lowercase the word “Title”:
sed 's/Title/\L&/' file.txt
\Lbegins lowercase mode.&represents the matched text.- To uppercase a whole match, use
\Uinstead.
Sometimes you’ll want to replace only the second or third match on a line. sed supports addressing individual matches with an extra number. For example:
sed 's/foo/bar/2' file.txt
Only the second occurrence of “foo” on each line is changed.
Finally, to prefix all non-empty lines with a character like “>”:
sed '/./s/^/> /' file.txt
/./matches non-empty lines.s/^/> /adds a prefix to the start of each matched line.
These advanced examples show how sed can adapt to complex needs, whether modifying structured data, filtering based on content, or rearranging lines. Mastery of sed on Debian 12 can significantly boost your efficiency in shell scripting, log analysis, and system administration tasks.
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.