1

How can I insert a line before each multiline range block and indent the blocks in gnu sed on windows? The files contain many code blocks of various length starting and ending with ```. Answers to many similar questions using a single line pattern don't use ranges. There are two similar questions: Insert line below text range with sed and sed: Appending after a block.

This code indents all code blocks as required:

sed '/```/,/```$/ s/\(.*\)/ \1/' test.md

I don't understand how to insert the === line before each code block but I understand grouping with {} is required to process the block after inserting the line. The questions above seem more complex requiring a buffer but this file should be able to be processed sequentially without a buffer.

I expect something like this attempt should work with the newline detail before the group:

sed '/```/,/```$/ {s/\(.*\)/ \1/}' test.md

test.md

a
b
```
c
d
e
```
f
g

Required

a
b
===
    ```
    c
    d
    e
    ```
f
g
flywire
  • 1,155
  • 1
  • 14
  • 38

1 Answers1

2

With GNU sed:

$ cat file
1
```
2
```
3
```
4
```
5
$ sed '/^```$/{ s/^/===\n    /; :a; n; s/^/    /; /^    ```$/b; ba; }' file
1
===
    ```
    2
    ```
3
===
    ```
    4
    ```
5

(The cool sedsed can show how it works in details.)


With awk:

$ awk '/^```/ { if (!f) { print "===" } f++ } f { print "    " $0 } f==2 { f=0; next } !f' file
1
===
    ```
    2
    ```
3
===
    ```
    4
    ```
5
pynexj
  • 19,215
  • 5
  • 38
  • 56
  • Nice. Comments 1) GNU sed on Windows requires ^^(ie escaped) 2) if code language given after ``` remove first $. Can you describe new `sed` commands? – flywire Oct 28 '21 at 04:30
  • "new" sed command? "the" sed command? please use the sed debugger [sedsed](https://github.com/aureliojargas/sedsed). – pynexj Oct 28 '21 at 06:59