Skip to main content

Debugger and code formatter for sed scripts

Project description

sedsed

Debugger and code formatter for sed scripts, by Aurelio Jargas.

Website: https://aurelio.net/projects/sedsed/

Download / Install

Sedsed is available as a pip package, just install it:

pip install --user sedsed

Alternative: sedsed is a single file application, so you can also just download and run sedsed.py. Note that you'll also need to download its only external requirement, sedparse.py.

Code formatting for sed scripts

Sedsed can turn cryptic oneliners into readable indented code:

$ sedsed --indent ':a;s/<[^>]*>//g;/</{N;ba;}'
:a
s/<[^>]*>//g
/</ {
    N
    b a
}

Convert your script to HTML to get code formatting, beautiful syntax highlighting and clickable links for GOTO commands (b and t).

$ sedsed --htmlize -f myscript.sed > myscript.html

See many examples of HTML-converted scripts in http://sed.sourceforge.net/local/scripts/

Debugging sed scripts

Sedsed allows you to debug your sed scripts. It inserts extra sed commands into the script and uses the system's sed to execute the modified script.

The added commands won't affect the script's original logic, but will show debug information while sed is running the script: the current command is printed, as well as the contents of both PATTERN SPACE and HOLD SPACE buffers, before and after every command.

With that information at hand, you can see how sed operates under the curtains.

For example, compare the normal sed run and the sedsed debug run for a script to reverse the line order (similar to Unix tac):

$ echo -e 'A\nB\nC' | sed '1!G;h;$!d'
C
B
A
$ echo -e 'A\nB\nC' | sedsed --debug '1!G;h;$!d'
PATT:A$
HOLD:$
COMM:1 !G
PATT:A$
HOLD:$
COMM:h
PATT:A$
HOLD:A$
COMM:$ !d
PATT:B$
HOLD:A$
COMM:1 !G
PATT:B\nA$
HOLD:A$
COMM:h
PATT:B\nA$
HOLD:B\nA$
COMM:$ !d
PATT:C$
HOLD:B\nA$
COMM:1 !G
PATT:C\nB\nA$
HOLD:B\nA$
COMM:h
PATT:C\nB\nA$
HOLD:C\nB\nA$
COMM:$ !d
PATT:C\nB\nA$
HOLD:C\nB\nA$
C
B
A

The input is three lines A, B and C, and the output is those three lines reversed: C, B, A. You can see how the sed buffers (PATT and HOLD) changed after every command (COMM).

Another example, a script to remove all HTML tags. It even removes tags that span in multiple lines.

$ cat menu.html
<a
   class="menu"
   href="index.html">Home</a>
$ cat menu.html | sed -e ':a;s/<[^>]*>//g;/</{N;ba;}'
Home
$ cat menu.html | sedsed --debug --hide=hold -e ':a;s/<[^>]*>//g;/</{N;ba;}'
PATT:<a$
COMM::a
COMM:s/<[^>]*>//g
PATT:<a$
COMM:/</ {
COMM:N
PATT:<a\n   class="menu"$
COMM:b a
COMM:s/<[^>]*>//g
PATT:<a\n   class="menu"$
COMM:/</ {
COMM:N
PATT:<a\n   class="menu"\n   href="index.html">Home</a>$
COMM:b a
COMM:s/<[^>]*>//g
PATT:Home$
COMM:/</ {
PATT:Home$
Home

You can see in the PATT lines how the multiline <a> tag is accumulated before the s command can remove the whole tag at once, leaving only its contents: Home.

Note that the --hide=hold option was used to avoid showing the contents of the HOLD SPACE buffer. It would be empty all the way, since this script does not use that extra buffer. You can also hide the PATT and COMM lines, if necessary.

For tricky scripts, sometimes it helps to only see the contents of the PATTERN SPACE buffer changing, so you can get a sense of how it is manipulated during execution. Using --hide=hold,comm you can achieve that. The next example uses that to show how ABC turned into CBA in this nice script to reverse strings (similar to Unix rev):

$ echo ABC | sedsed --debug --hide=hold,comm \
    -e '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//' | uniq
PATT:ABC$
PATT:ABC\n$
PATT:ABC\nBC\nA$
PATT:BC\nA$
PATT:BC\nC\nBA$
PATT:C\nBA$
PATT:C\n\nCBA$
PATT:\nCBA$
PATT:CBA$
CBA

In those examples the sed script was informed as an argument using the -e option. Sedsed also supports the -f option to inform a sed script file, and the traditional -n option to run in quiet mode.

Enjoy!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sedsed-2.0.0.tar.gz (15.8 kB view details)

Uploaded Source

Built Distribution

sedsed-2.0.0-py2.py3-none-any.whl (27.8 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file sedsed-2.0.0.tar.gz.

File metadata

  • Download URL: sedsed-2.0.0.tar.gz
  • Upload date:
  • Size: 15.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.6.8

File hashes

Hashes for sedsed-2.0.0.tar.gz
Algorithm Hash digest
SHA256 6f8d70970b493efb84f9fbdefb801d37041b3b0da0ef49ce2acb2babb643f0b1
MD5 4deaf63bdc1220d7026f840d2cf6f43f
BLAKE2b-256 a954f5afad0709b74d65340bb3e31230ad17cd91e448d9cd9349eb113fb44eb0

See more details on using hashes here.

File details

Details for the file sedsed-2.0.0-py2.py3-none-any.whl.

File metadata

  • Download URL: sedsed-2.0.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 27.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.6.8

File hashes

Hashes for sedsed-2.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 1d005baaf3ad071afde37fa1c783c9621e6e332dcd0c553ac308b0b5b2617ccd
MD5 e90c4fdbcb96ce59b4a85f5bbedea3fb
BLAKE2b-256 a76760578e69e9a1df0d56be1fbcab1708bae6659c34e346c2417dd2694753bf

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page