Skip to main content
Correct erroneous `grep` regex. Thanks Stephane!
Source Link
jubilatious1
  • 3.9k
  • 10
  • 21

Using Raku (formerly known as Perl_6)

~$ raku -ne 'put join "", m:g/ ban <[a..z]>* /;' file #OR ~$ raku -ne 'put join "", m:g/ ban <alpha>* /;' file 

Raku is a programming language in the Perl-family. It features high level support for Unicode, built in. The first answer above closely matches the excellent Perl answer posted by @steeldriver. The second answer uses Raku's pre-defined <alpha> character class, which includes upper/lower case (Unicode) letters plus underscore. Above the matching words are joined on "" empty-string, but they could just as easily be joined on a single whitespace by changing empty-string to " ".

A more powerful way of approaching this problem is to use Raku's words routine, which breaks input text on whitespace. Below uses the same -ne linewise non-autoprinting flags as above, except this time Raku's grep is employed. The first example looks similar to the code above, while the second example is for fans of method-chaining. Thanks to @StéphaneChazelas for pointing out the necessity of adding ^/$ anchors:

~$ raku -ne 'put join "", .words.grep( /^ ban <alpha>* $/);' file #OR ~$ raku -ne '.words.grep( /^ ban <alpha>* $/).join.put;' file 

Sample Input:

banana apple banana apple bananas banned 

Sample Output (all four code examples above):

bananabanana bananasbanned 

Note: the final 2 "grep words" code examples can be inverted to give non-matching words, by using either .grep(none /^ ban <alpha>* $/) or .grep({ ! /^ ban <alpha>* $/ }).

https://doc.raku.org
https://raku.org

Using Raku (formerly known as Perl_6)

~$ raku -ne 'put join "", m:g/ ban <[a..z]>* /;' file #OR ~$ raku -ne 'put join "", m:g/ ban <alpha>* /;' file 

Raku is a programming language in the Perl-family. It features high level support for Unicode, built in. The first answer above closely matches the excellent Perl answer posted by @steeldriver. The second answer uses Raku's pre-defined <alpha> character class, which includes upper/lower case (Unicode) letters. Above the matching words are joined on "" empty-string, but they could just as easily be joined on a single whitespace by changing empty-string to " ".

A more powerful way of approaching this problem is to use Raku's words routine, which breaks input text on whitespace. Below uses the same -ne linewise non-autoprinting flags as above, except this time Raku's grep is employed. The first example looks similar to the code above, while the second example is for fans of method-chaining:

~$ raku -ne 'put join "", .words.grep( / ban <alpha>* /);' file #OR ~$ raku -ne '.words.grep( / ban <alpha>* /).join.put;' file 

Sample Input:

banana apple banana apple bananas banned 

Sample Output (all four code examples above):

bananabanana bananasbanned 

https://doc.raku.org
https://raku.org

Using Raku (formerly known as Perl_6)

~$ raku -ne 'put join "", m:g/ ban <[a..z]>* /;' file #OR ~$ raku -ne 'put join "", m:g/ ban <alpha>* /;' file 

Raku is a programming language in the Perl-family. It features high level support for Unicode, built in. The first answer above closely matches the excellent Perl answer posted by @steeldriver. The second answer uses Raku's pre-defined <alpha> character class, which includes upper/lower case (Unicode) letters plus underscore. Above the matching words are joined on "" empty-string, but they could just as easily be joined on a single whitespace by changing empty-string to " ".

A more powerful way of approaching this problem is to use Raku's words routine, which breaks input text on whitespace. Below uses the same -ne linewise non-autoprinting flags as above, except this time Raku's grep is employed. The first example looks similar to the code above, while the second example is for fans of method-chaining. Thanks to @StéphaneChazelas for pointing out the necessity of adding ^/$ anchors:

~$ raku -ne 'put join "", .words.grep( /^ ban <alpha>* $/);' file #OR ~$ raku -ne '.words.grep( /^ ban <alpha>* $/).join.put;' file 

Sample Input:

banana apple banana apple bananas banned 

Sample Output (all four code examples above):

bananabanana bananasbanned 

Note: the final 2 "grep words" code examples can be inverted to give non-matching words, by using either .grep(none /^ ban <alpha>* $/) or .grep({ ! /^ ban <alpha>* $/ }).

https://doc.raku.org
https://raku.org

Source Link
jubilatious1
  • 3.9k
  • 10
  • 21

Using Raku (formerly known as Perl_6)

~$ raku -ne 'put join "", m:g/ ban <[a..z]>* /;' file #OR ~$ raku -ne 'put join "", m:g/ ban <alpha>* /;' file 

Raku is a programming language in the Perl-family. It features high level support for Unicode, built in. The first answer above closely matches the excellent Perl answer posted by @steeldriver. The second answer uses Raku's pre-defined <alpha> character class, which includes upper/lower case (Unicode) letters. Above the matching words are joined on "" empty-string, but they could just as easily be joined on a single whitespace by changing empty-string to " ".

A more powerful way of approaching this problem is to use Raku's words routine, which breaks input text on whitespace. Below uses the same -ne linewise non-autoprinting flags as above, except this time Raku's grep is employed. The first example looks similar to the code above, while the second example is for fans of method-chaining:

~$ raku -ne 'put join "", .words.grep( / ban <alpha>* /);' file #OR ~$ raku -ne '.words.grep( / ban <alpha>* /).join.put;' file 

Sample Input:

banana apple banana apple bananas banned 

Sample Output (all four code examples above):

bananabanana bananasbanned 

https://doc.raku.org
https://raku.org