3 ms·
Not 100% tested, but should hopefully work: find . -name go.mod | xargs gawk -i inplace '/^replace \(/{d=1}; (!d && !/^replace/); /^\)/{d=0}' Explanation
by akavel 5y ago
Not 100% tested, but should hopefully work:
find . -name go.mod | xargs gawk -i inplace '/^replace \(/{d=1}; (!d && !/^replace/); /^\)/{d=0}'
Explanation of gawk arguments:
— -i inplace — https://stackoverflow.com/a/16531920 https://stackoverflow.com/a/16531920
— /^replace \(/{d=1} — on lines starting with `replace (`, set a variable named `d` (any var name would work, 'd' is my mental shortcut for 'delete' here) to value `1` (i.e. "true"); Note: in awk, unset variables default to 0 ("false") value
— (!d && !/^replace/) — on lines where `d` is 0 (which is default value of unset variables in awk), and which don't start with `replace` pattern, print the line. Default action when unspecified in awk is `{print}`, this entry is a shortcut of: (!d && !/^replace/) {print}
— /^\)/{d=0} — on lines starting with `)`, reset `d` variable back to 0 ("false")
In other words, this encodes: "delete any lines between `replace (` and nearest `)`, and also delete any other lines starting with `replace`"
- bithavoc 5y agoNice, learned a few things about awk here.