Unleash your inner root

1

sed: A Guide for Mere Mortals: 01

Software      : GNU SED
Version       : 4.2.1
Compatibility : Any Copy of SED

Introduction

I am a huge fan of thinking of myself as some kind of omni-powerful being and it is for that very reason that I became a Linux User/Admin. Among the myriad of tools available to the discerning power user few tools pack so much raw simple power as the sed command. More to the point: few things other than sed can make you seem more like a great and powerful wizard to an outside eye as being able to listen to a problem presented to you, and instantly being able to reply with a string of magical sed/regex that when pointed at a file fix’s all of their problems.

The problem? Learning sed is surprisingly hard.

For this guide I will start at the beginning and move on to more complicated incantations that hopefully will help illuminate the way to the higher state of mind that is #root.

What is Sed?

First what is sed? sed is simply put  a stream editor. It takes a stream of text passes it through the editing rules you have crafted for it and makes changes to said text as necessary.

The easiest realm of sed to start comprehension with is the substitute command, but before we get to that I’d like to break down sed’s basic formatting. A single sed line at the terminal level can be broken down into three pieces of information:

  1. The command: The command is just sed… This is a no brainer, just adding it for the sake of completeness
  2. The options:  also fairly simple these are your standard unix – options that you put on just about anything, there are a lot of them, we’ll cover them as time goes by.
  3. The script: The script is the important bit, the script is contained within two: —-> ‘ <—- and tells sed exactly what you, the great and powerful wizard, would like sed to do.
  4. (optional) target: The target is where sed is drawing the text from to perform its magic on. This can be a file, or input can be piped into sed from another command.

All together your sed structure looks something like: sed -<option> ‘<sed magic goes here>’ <file>

At a basic usage level (advanced usage levels may vary, but its not important for the time being) the script can be thought of to be broken down into two sides: The pattern and the action.

The pattern is a matching game, its what you are telling sed to look for in the text you are feeding it. The target of your spell if you will. The action is, surprise surprise, the action you want to take on the pattern.

Substitution

That being sed(haha), lets move on to SUBSTITUTION! You may have noticed, or not, that the last few times I’ve typed substitue I’ve bolded the s. That is simply because s is the substitution command in sed… easy to remember.

At a basic level it looks like this:

sed 's/pattern/Replacement pattern/'

As a practical example if you were to enter the following into the terminal:

 echo The best pets in the world are cats! | sed 's/cats/dogs/'

You would get a line of text that says:

The best pets in the world are dogs!

The catch: By default that sed string will substitute the first word on each line of input only. 

Going Global

If you’d like sed to change all values on a line, you must add the global flag at the end.

echo cats cats cats! | sed 's/cats/dogs/'
dogs cats cats!
echo cats cats cats! | sed 's/cats/dogs/g'
dogs dogs dogs!

And that’s is all it takes to do basic substitution. Now to make it relevant. introducing… –i. -i option will edit a file in place. So:

sed -i 's/cats/dogs/g' pets.txt

Will take in the file pets.txt and replace all occurrences of “cats” with “dogs”.

 

Thanks for joining me on this sedtastic journey. Though this first step is fairly simple, fear not, for great things are built on simple foundations!
The next installment of “Sed: A guide for mere mortals” is now available!

linuxsed

Brandon.Graves • October 12, 2015


Next Post

Comments

  1. rootchord October 14, 2015 - 3:08 pm Reply

    Very good explanation. Looking forward to the next one!

Leave a Reply

Your email address will not be published / Required fields are marked *

%d bloggers like this: