3 ms·
This problem is easily solved with a regexp that processes the input as a whole, rather than working on it line by line. I had this need often enough that I ex
by orivej 8y ago
This problem is easily solved with a regexp that processes the input as a whole, rather than working on it line by line. I had this need often enough that I exported Go regexp engine as a command line tool regrep, which can insert "elephant" after "baz" with:
go get github.com/orivej/unix/regrep
regrep s '(\n( *-) baz\n)' $'$1$2 elephant\n' < input.yaml
It only processes standard input, and can not by itself replace the contents of an input file with its output; but another tool, inplace, helps:
go get github.com/orivej/unix/inplace
find . -name '*.yaml' -exec inplace {} regrep s '(\n( *-) baz\n)' $'$1$2 elephant\n' \;
- hawski 8y agoIt's close to the idea of structural regular expressions [0]. I'm still waiting for the awk from the paper. [0] http://doc.cat-v.org/bell_labs/structural_regexps/ http://doc.cat-v.org/bell_labs/structural_regexps/