Any way to have a "verbose mode" or "debug mode" with sed?

10,915

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.

Share:
10,915

Related videos on Youtube

don_crissti
Author by

don_crissti

Updated on September 18, 2022

Comments

  • don_crissti
    don_crissti over 1 year

    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.

    • fra-san
      fra-san over 5 years
      It looks like GNU sed gained a --debug feature last summer (version 4.6).