gymnogyps
gymnogyps

Reputation: 11

Need a greedy address range in sed


I have a bash script and I'm working with Vim. This script appends data to a file before the end of the final fold by copying the file with the final # }}} then appending the new data followed by a new # }}}. This following snippet could be so much more elegant I had a greedy address range.

local END=$(grep -n '# }}}'  $FILENAME   | sed -n "$ s/\([[:digit:]]*\)\(.*\)/\1/p ")
let END=$END-1
sed -n "1, $END {p}" $FILENAME  > $TEMPFILE

In theory if sed supported a '--greedy-address-range' flag I could use this: sed --silent --in-place --greedy-address-range "1, /# }}}/ {p}" $FILENAME

Of course, thank you in advance for any suggestions!

Upvotes: 1

Views: 263

Answers (1)

Gilad Merran
Gilad Merran

Reputation: 61

If I understood well the output you need, this will do the job just as well:

tac $FILENAME | sed -n '/# }}}/,$p' | tac > $FILENAME

In order to print all lines until the last match, I reverse the file and then use sed to print all lines from the first match to EOF then reverse it again.

Upvotes: 1

Related Questions