Debugging For Sed — No Kidding

If you do much Linux shell scripting, you’ve probably encountered sed — the stream editor — in an example. Maybe you’ve even used it yourself. If all you want to do is substitute text, it is easy and efficient. But if you try to do really elaborate editing, it is often difficult to get things right. The syntax is cryptic and the documentation is lacking. But thanks to [SoptikHa2] you can now debug sed scripts with a text-based GUI debugger. Seriously.

According to the author, the program has several notable features:

  • Preview variable values, both of them!
  • See how will a substitute command affect pattern space before it runs
  • Step through sed script – both forward and backward!
  • Place breakpoints and examine program state
  • Hot reload and see what changes as you edit source code
  • Its name is a palindrome

There’s only one word for that last feature: wow.

Honestly, we are more likely to reach for awk or Perl if we have heavy lifting, but we know that’s a lot of overhead for many tasks. Being able to see what’s really happening inside sed might make us a little less reticent to do things with sed in the future.

The code is in Rust and it uses a debugging interface that has always been in sed. Who knew? If you want more details, the author has a blog post about the program that goes into a bit of detail. The built-in debugging interface gives you a lot of internal state information but omits one thing: the command that is executing. So the debugger has to simulate part of sed to keep track of what statement is executing.

If you need debugging on the rest of your script, relax. Or, try avoiding the bugs in the first place.

13 thoughts on “Debugging For Sed — No Kidding

  1. If you have a repetitive job, it’s worth testing sed vs awk. Sed’s usually faster. It can get cryptic very fast, but it is also fast.

    I’ll be the first to drop the old chestnut:

    1) you have a problem
    2) you solve it with a regex
    3) now you have two problems

  2. It can be difficult to determine whether a problem can be solved with regular expressions and you could waste an hour easily trying to find a combination that works. AWK in the meantime is trivially turing complete, it will work, it’s just a matter of effort.

        1. Well desed is the tool, used deseddling to mean generically doing something with it, either writing it, or about it, or using it. Just to ryhme with meddling, from the Scooby Doo quote, So I could use “curses” referring to the textmode library which enables TUI on Vax/BSD and as ncurses on Linux. My jokes aren’t nearly as funny when I have to explain them.

    1. Well I have heard the term TUI before I don’t think it’s instantly recognizable for many people. Where is I think almost everyone can intuit what a text GUI is.

  3. One of the funniest I’ve seen was one of the Microsoft centric IT guys in the department looking over my shoulder while I was writing a busybox script in ash and using sed to extract and parse serial GPS data. He wanted to know the command line arguments for sed. I handed him the 394 page ‘sed and awk’ book.

Leave a Reply to HorstBaerbelCancel reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.