Sunday, October 9, 2022

[SOLVED] AWK commands giving different results in different Ubuntu versions

Issue

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=

Solution

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:

/([[:alpha:]]+|[[:digit:].-]+|[^[:alnum:].-]+)/

to this:

/([a-zA-Z]+|[0-9.-]+|[^a-zA-Z0-9.-]+)/

e.g.:

echo "1.5GB" |awk '{ gsub(/([a-zA-Z]+|[0-9.-]+|[^a-zA-Z0-9.-]+)/,"&\n",$0) ; print "size="$1"\nsymbol="$2}'
size=1.5
symbol=GB


Answered By - Ed Morton
Answer Checked By - Pedro (WPSolving Volunteer)