Unleash your inner root

`sed`: A Guide for Mere Mortals: 03

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

Previously on “Sed: A guide for mere Mortals”
Hello and Welcome to another Sed guide for mere mortals. This one is going to be pretty short and sweet and to the point. (And by that I of course mean “Far longer than I originally intended”)

Up to this point, we’ve talked about using the substitute function with sed. Which is good! I mean really, substitute is what the *average* IT person uses sed for a majority of the time… But remember. If That one little function was all sed could do for you, it would not be treated with the near mythical mystique that it is!

So what else can `sed` do for you? Well… It can’t make you fresh pancakes, I can tell you that!

First let me introduce to you the means of managing your pattern WITHOUT using the substitute command. That is done simply with two “/”‘s with a pattern in them, very similar to the formatting of substitute. Something like:

sed '/magic/' file.txt

Which would of course look for the pattern “magic” inside “file.txt”

so once you’ve found your pattern…what can you do with it? Well…you can…EDIT IT! WEEEEEEE.

For example, in my “/etc/ntp.conf” file, I have the line:

server ntp1.metashell.net iburst"

which is fine and dandy, but I just finished ntp2.metashell.net, and want to append it after the initial entry. Which is of course beyond simple!

sed -i '/ntp1/ a\ntp2.metashell.net iburst' /etc/ntp.conf

will append the word “test” to the line after the pattern of “ntp1”.
You can also do the opposite with the insert command.

sed -i '/ntp1/ i\ntp2.metashell.net iburst' /etc/ntp.conf

Will insert “ntp2.metashell.net iburst” to the line BEFORE the line containing the pattern ntp1.

Easy. Alternatively, if 3 months after that, your ntp2 server goes down, and you want to go back to using only ntp1, you can simply use the delete function!

sed -i '/ntp2/ d' /etc/ntp.conf

A note of caution: Sed is of course greedy. It seeks to follow your instructions TO THE LETTER. If your pattern space matches multiple lines, your instructions WILL BE carried out on each line, for example imagine a file:

The orange dog went 3rd street
The cat went to the market
The Cat bought 3 toy's!

You think to yourself, the dog going to third street doesn’t really fit in this story, so you run a quick:

sed -i '/3/ d' story.txt

The problem is, the pattern “3” matches two different lines! You then end up with:

The cat went to the market

As your final document, which is of course not what you want. Instead you should have likely used “dog” as your pattern space.

As always: think before you sed, but also try to incorporate these couple of tricks into your day to day admin life and you will be `sed`ing with the best of them! The more often you use it, the more easily those little sed tricks will come to you when you need them.

All in all, I know these seem pretty simple, but trust me, we’re building a foundation here that will open up some pretty amazing doors in time! Don’t be that guy that googles a specific sed functionality every time they need to do some cool edit somewhere. Learn what each component in sed does, take it into practice, and you will be able to rattle of some awesome things before you know it!

The 4th installment of A guide for Mere Mortals is GO!

linuxregexsed

Brandon.Graves • December 15, 2015


Previous Post

Next Post

Leave a Reply

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

%d bloggers like this: