Counting Fields with awk

ok, so a user can specify options as is shown below:

ExA:
cpu.pl!23!25!-allow

or

ExB:
cpu.pl!23!25!-block!all

options are delimited by the exclamation mark.

now, in example A, there are 4 options provided by the user.

in example B, there are 5 options provided by the user.

now, users can provide as many options as they want.

how can i count the options provided by the user?

im thinking:

echo "$options" | awk -F"!" .....

linux/sunos

echo "$options" | awk -F"!" '{ print NF }'
1 Like
echo "$options" | awk -F\! '{ print NF }'
1 Like

A shell-only alternative:

$ a='cpu.pl!23!25!-block!all'
$ oldIFS="$IFS"
$ IFS='!'
$ set -- $a
$ echo $#
5
$ a='cpu.pl!23!25!-allow'
$ set -- $a
$ echo $#
4
$ IFS="$oldIFS"
1 Like