[bash] Use Awk to extract substring

Given a hostname in format of aaa0.bbb.ccc, I want to extract the first substring before ., that is, aaa0 in this case. I use following awk script to do so,

echo aaa0.bbb.ccc | awk '{if (match($0, /\./)) {print substr($0, 0, RSTART - 1)}}'

While the script running on one machine A produces aaa0, running on machine B produces only aaa, without 0 in the end. Both machine runs Ubuntu/Linaro, but A runs newer version of awk(gawk with version 3.1.8 while B with older awk (mawk with version 1.2)

I am asking in general, how to write a compatible awk script that performs the same functionality ...

This question is related to bash awk

The answer is

You just want to set the field separator as . using the -F option and print the first field:

$ echo aaa0.bbb.ccc | awk -F'.' '{print $1}'

Same thing but using cut:

$ echo aaa0.bbb.ccc | cut -d'.' -f1

Or with sed:

$ echo aaa0.bbb.ccc | sed 's/[.].*//'

Even grep:

$ echo aaa0.bbb.ccc | grep -o '^[^.]*'

Similar questions with bash tag:

Similar questions with awk tag: