Greetings, fellow tech enthusiasts and code conjurers! Today, we're diving into the mystical realm of Sed, the incredibly powerful stream editor. You're about to embark on a fantastical adventure to explore the untapped potential of this text-processing wizardry. So, let's conjure up some fun and unravel the secrets of Sed!
Before we embark on our journey, let's take a quick trip down memory lane. Sed was created by Lee E. McMahon in 1973-74 while he was working at Bell Labs. It was first released in Version 2 Unix . The name "Sed" is derived from the Latin word "sed" (pronounced "sed"), which means "but" or "however" - a subtle indication that it's a tool meant for transformations!
If you're running macOS or a Linux distribution, you're in luck! Sed is most likely already installed on your system . You can verify by running sed --version
in your terminal.
For Windows users, don't worry! You can acquire the power of Sed through these methods:
The basic syntax of Sed looks like this:
sed 's/old-text/new-text/g' input-file > output-file
Let's decode this incantation:
s
: Substitute command. old-text
: The text you want to replace. new-text
: The text you want to replace the old text with. g
: Apply the substitution globally (to every occurrence). input-file
: The file you want to work on. output-file
: The file to save your changes to. Here's a simple example to illustrate the power of Sed:
echo "I am the king of this magical kingdom! Huzzah!" | sed 's/king/queen/g'
The output will be:
I am the queen of this magical kingdom! Huzzah!
Deleting lines: To delete a line, use the d
command.
sed 'nd' input-file > output-file
Where n
is the line number you wish to delete.
Inserting text: To insert text before a specific line, use the i
command.
sed 'n i\text-to-insert' input-file > output-file
Appending text: To append text after a particular line, use the a
command.
sed 'n a\text-to-append' input-file > output-file
Sed's magic is amplified when combined with regular expressions! They work like a charm when paired with Sed's substitution command. Let us enchant you with an example:
echo "I love my cat. My cat is awesome" | sed 's/my [a-z]*\( \|\.\)/goblin\1/g'
The output will be:
I love goblin. Goblin is awesome
This incantation casts a spell to replace 'my' followed by any word (in lowercase) with 'goblin'. The regular expression magic lies within the [a-z]*\( \|\.\)
potion.
Let's unveil some practical sed spells!
sed 's/192\.168\.0\.\([0-9]\+\)/10.0.0.\1/g' input-file > output-file
This spell updates all occurrences of IP addresses starting with 192.168.0
to start with 10.0.0
instead.
sed 's/ \+/ /g' input-file > output-file
This enchantment tidies up your text files by replacing multiple consecutive spaces with single spaces. Space-sparsity achieved!
You've come a long way, apprentice! As you continue on your path to mastering Sed, consider exploring these advanced topics:
-i
flagRemember, practice makes perfect, and there's always more to uncover in the mystical world of text-processing sorcery! So, keep exploring, experimenting, and conjuring up your own magical transformations with Sed!
Grok.foo is a collection of articles on a variety of technology and programming articles assembled by James Padolsey. Enjoy! And please share! And if you feel like you can donate here so I can create more free content for you.