Using Raku (formerly known as Perl6)
~$ echo "abc " | raku -ne '.contains(/ \s /).say'
True
~$ echo "abc" | raku -ne '.contains(/ \s /).say'
False
The above Raku code is run linewise over the input with the awk-like -ne
command-line flags. Raku's contains
method returns a boolean. The leading .
dot on contains
directs that input is taken off the command line, or (alternatively) stdin.
~$ echo "abc " | raku -ne 'say .contains(/ \s /) ?? True !! False;'
True
~$ echo "abc" | raku -ne 'say .contains(/ \s /) ?? True !! False;'
False
Above is slightly more complicated because it uses Raku's ternary operator: Test ??
True !!
False . Raku has logical True
and logical False
, so no need to quote the above returns. The advantage here is you can simply replace True
and False
with double-quoted returns of your choosing, e.g. "Yes"
and "No"
.
Presumably the OP's question pertains to horizontal whitespace, and in that regard Raku can distinguish \h
horizontal whitespace from \v
vertical whitespace:
~$ raku -e 'put "abc\t";' | raku -ne 'say .contains(/ \h /);'
True
~$ raku -e 'put "abc\t";' | raku -ne 'say .contains(/ \v /);'
False
The OP doesn't say whether multiline input strings must be handled. They'll always be "positive" for whitespace, but possibly "negative" for horizontal whitespace. [Think of a column of numbers as input]. Anyway, in Raku you can read input in linewise as above (which autochomps by default), or all-at-once (retaining eol \n
newlines) with the oddly-named-but-memorable slurp
.
Reading linewise (autochomps):
~$ raku -e 'put "1\n2\n3";' | raku -ne 'say .contains(/ \h /);'
False
False
False
~$ raku -e 'put "1\n2\n3";' | raku -e 'for lines() {say .contains(/ \h /)};'
False
False
False
Reading all-at-once (no autochomping):
~$ raku -e 'put "1\n2\n3";' | raku -e 'say slurp.contains(/ \v /);'
True
~$ raku -e 'put "1\n2\n3";' | raku -e 'put slurp.contains(/ \h /);'
False
Addendum: I'm interpreting the OP's statement, "I do not have to worry about things outside of ASCII..." as 'I don't care if Unicode is handled or not'. If only ASCII whitespace is to be handled (and all others rejected), that's something Raku can manage, but not addressed above. Note that Raku is Unicode-ready, so that \s
(which is short for <space>
) and \h
(which is short for <blank>
) as well as \v
all accept Unicode by default.
If you want to reject non-ASCII (horizontal) whitespace, you could try something like the following bespoke character class: <:ASCII> & <blank>
.
Examples:
~$ raku -e 'put "\xA0";' | raku -ne 'put .contains(/ <blank> / );'
True
~$ raku -e 'put "\xA0";' | raku -ne 'put .contains(/ <:ASCII> & <blank> / );'
False
https://docs.raku.org/language/operators#index-entry-operator_ternary
https://docs.raku.org/language/regexes#\h_and_\H
https://docs.raku.org/routine/contains
https://raku.org
#!/bin/sh
and the variable set withstring="abc\nxyz"
it seems to incorrectly respond withno character classified as whitespace
? I get the same behavior with AdminBee's answer. My full example script using this answer is here: https://gist.github.com/chrissound/762667fba754dc1a472a6386cca124f0 – Chris Stryczynski Jan 26 '23 at 16:56n
. Usestring=$(printf 'abc\nxyz')
to get a newline character in there. Orstring=$'abc\nxyz'
with somesh
implementations (will be in the next POSIX version). Or a literal newline. – Stéphane Chazelas Jan 26 '23 at 17:23[[ "$string" =~ [[:blank:]] ]]
is bash syntax, not sh syntax (also supported by ksh93 ([[...]]
comes from ksh) and zsh). – Stéphane Chazelas Jan 26 '23 at 17:31