We are using below awk
commands to split numbers and alphabets in a alphanumeric text.
echo "1.5GB" |awk '{ gsub(/([[:alpha:]]+|[[:digit:].-]+|[^[:alnum:].-]+)/,"&n",$0) ; print "size="$1"nsymbol="$2}'
This command gives desired result in Ubuntu 20.04
. Result is
size=1.5
symbol=GB
But in Ubuntu 18.04
it gives below result,which is not a desired result
size=1.5GB
symbol=
3
Answers
i can’t replicate the issue – all my
awk
‘s outputs ended up with the same hashed value :Although it is unclear what change in mawk 1.3.4 versus 1.3.3 made your code work, the code is logically flawed to begin with if the intent is to display the numeric portion of the input as
size
and the alphabetical portion assymbol
even when one of the two components is missing, since the call togsub
makes whichever alphabetical or numeric characters it gets the first field. For example, if the input is justGB
, your code will output:which I don’t think is desired.
A better approach is to remove the alphabetical portion from the input to make it
size
, and remove the numeric portion from the input to make itsymbol
:That 1996 mawk is a minimal-featured version of awk designed for speed of execution. It’s not POSIX compliant and so shouldn’t be expected to support POSIX character classes. Get a new version if at all possible or change this:
to this:
e.g.: