regex - Indent and wrap consecutive matching lines with string -


i convert predictably-formatted file containing code snippets markdown. file looks this:

my code snippets          2015-05-01  file contains useful code snippets every day usage in linux command line.  sed   sed 's/\(.*\)1/\12/g'                  # modify anystring1 anystring2   sed '/^ *#/d; /^ *$/d'                 # remove comments , blank lines  sort   sort -t. -k1,1n -k2,2n -k3,3n -k4,4n   # sort ipv4 ip addresses  ... 

lines starting sed or sort (lowercase - may have whitespace in front) should wrapped ``` (markdown starting / ending code markers), indented 4 spaces , have 1 blank line before , after section. consecutive lines sed or sort should wrapped inside same coding section. final markdown file should this:

my code snippets          2015-05-01  file contains useful code snippets every day usage in linux command line.  sed      ```     sed 's/\(.*\)1/\12/g'                  # modify anystring1 anystring2     sed '/^ *#/d; /^ *$/d'                 # remove comments , blank lines     ```  sort      ```     sort -t. -k1,1n -k2,2n -k3,3n -k4,4n   # sort ipv4 ip addresses     ``` 

i interested in awk/sed/bash solution, other suggestions welcome.

maybe this:

awk '     $1 ~ /^(sed|sort)$/ {         print "\n    ```"         while ($1 ~ /^(sed|sort)$/) {             sub(/^[ \t]*/, "    ")             print             if (!getline) {                 print "    ```\n"                 exit             }         }         print "    ```\n"     }     1' 

output:

my code snippets          2015-05-01  file contains useful code snippets every day usage in linux command line.  sed      ```     sed 's/\(.*\)1/\12/g'                  # modify anystring1 anystring2     sed '/^ *#/d; /^ *$/d'                 # remove comments , blank lines     ```   sort      ```     sort -t. -k1,1n -k2,2n -k3,3n -k4,4n   # sort ipv4 ip addresses     ``` 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -