25

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.

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
don_crissti
  • 79,330
  • 30
  • 216
  • 245
  • Is `l;x;l;x;` at the start & end of the program adequate? – Michael Homer Jan 06 '19 at 19:36
  • 5
    It looks like GNU `sed` [gained](https://lists.gnu.org/r/sed-devel/2018-07/msg00006.html) a `--debug` [feature](https://www.gnu.org/software/sed/manual/sed.html#index-_002d_002ddebug) last summer (version 4.6). – fra-san Jan 06 '19 at 19:56

1 Answers1

25

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.

Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164