Given a class, such as
[:digit:]
I would like the output to be
0123456789
Note, the method should work for all POSIX character classes. Here is what I have tried
$ printf %s '[:digit:]'
[:digit:]
§ Character classes
I'm sure there's a better way but here's a brute force method:
for i in {0..127}; do
char=$(printf \\$(printf '%03o' "$i"))
[[ $char =~ [[:alpha:]] ]] && echo "$char"
done
Loop through all the decimal character values, convert them to the corresponding ASCII character and test them against the character class.
The range might be wrong but the check seems to work.
As others have mentioned in the comments, it is also possible to use the ==
operator instead of the =~
in this case, which may be slightly faster.
$ seq 126 | awk '{printf "%c", $0}' | grep -o '[[:digit:]]'
0
1
2
3
4
5
6
7
8
9
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With