Any way to have a “verbose mode” or “debug mode” with sed

debugginggnused

Is there a way to make gnu sed be verbose about what is run and what is done ?
I'd like to have something like a "debug mode" so that I can see – for each line of input – the content of the hold space and pattern space before and after the script is run etc.

Best Answer

As fra-san mentioned, GNU sed introduced a --debug option which does pretty much what you’re looking for, in version 4.6; so e.g if you run:

printf '%s\n' one two  | sed --debug 'H;1h;$x;$s/\n/_/g'

the output is

SED PROGRAM:
  H
  1 h
  $ x
  $ s/\n/_/g
INPUT:   'STDIN' line 1
PATTERN: one
COMMAND: H
HOLD:    \none
COMMAND: 1 h
HOLD:    one
COMMAND: $ x
COMMAND: $ s/\n/_/g
END-OF-CYCLE:
one
INPUT:   'STDIN' line 2
PATTERN: two
COMMAND: H
HOLD:    one\ntwo
COMMAND: 1 h
COMMAND: $ x
PATTERN: one\ntwo
HOLD:    two
COMMAND: $ s/\n/_/g
MATCHED REGEX REGISTERS
  regex[0] = 3-4 '
'
PATTERN: one_two
END-OF-CYCLE:
one_two

I don’t know what distribution you use, but this version of sed (or a later one) is available in Debian 10, in Ubuntu 19.04, and derivatives; it will be available in Fedora 33.

Related Question