2011-10-20 Computers

What I didn't know about sed all these years!

When I first learned about Unix many moons ago sed(1), the stream editor, was one of the tools to explore. I didn't find it particularly useful until now. How many times have I not wanted to filter out only a part of a file or a stream. Without knowing better I have used tools like awk(1) but more often written scripts in perl or python to do the job.

And now, years later, yes even decades later, I find that it has been there all the time in sed, the range concept! Look here:

sed -n '/start-regexp/,/end-regexp/p'

does the trick. All that is needed is there! Here is the details.

So /start-regexp/,/end-regexp/ defines a region in the stream and 'p' defines that it should be printed.

Voila! That is what we wanted. A simple way to select portions of a stream. Lets look at an example. This is how to get the output during one specific minute in the warn log:

sed -n '/Oct 20 15:59:/,/Oct 20 16:/p' < /var/log/warn

Really quite simple don't you think!