How can I replace multiple lines with line separators without greed?

I want to generate puzzles from appropriately formatted input strings.

Input Example: Foo was the +first+ to get a drink at +the bar+.

Required Conclusion: Foo was the _____ to get a drink at ___ ___.

With any standard shell tool, what is the easiest (by eye) solution for this?

+4
source share
3 answers

This one line awk should help you:

awk -F'+' -v OFS="" 'NF>2{for(i=2;i<=NF;i+=2)gsub(/\S/,"_",$i)}7'

Test

kent$  awk -F'+' -v OFS="" 'NF>2{for(i=2;i<=NF;i+=2)gsub(/\S/,"_",$i)}7' <<<"Foo was the +first+ to get a drink at +the bar+."
Foo was the _____ to get a drink at ___ ___.
+5
source

Your easy-to-eye test can be ... tense answers to this question.

Perl:

$ echo "$str" | perl -pe 's/\+(.*?)\+/ ($new=$1) =~ s{\w}{_}g; $new /eg'
Foo was the _____ to get a drink at ___ ___.
+1
source

, :

sed -r ':a;/\+[^+]*\+/!b;s//\n&\n/;h;s/.*\n(.*)\n.*/\1/;s/\+([^+]*)\+/\1/g;s/[^ ]/_/g;G;s/(.*)\n(.*)\n.*\n/\2\1/;ta'

:

$ echo "Foo was the +first+ to get a drink at +the bar+." | sed -r ':a;/\+[^+]*\+/!b;s//\n&\n/;h;s/.*\n(.*)\n.*/\1/;s/\+([^+]*)\+/\1/g;s/[^ ]/_/g;G;s/(.*)\n(.*)\n.*\n/\2\1/;ta'
Foo was the _______ to get a drink at ____ ____.

Mostly "stolen" from here: fooobar.com/questions/942691 / ...

0
source

All Articles