[ksh88 and awk] Number of fields with a value.

Hi,

With:

# VALUES="one~two~~~"
# echo $VALUES | awk 'BEGIN {FS="~"} {print NF}'
5

I can determine the number of fields.

How to determine the number of fields with a value ?
In this case 2.

Thanks in advance,

ejdv

Use FS="~+" instead of FS="~"

Thanks, but I get the same result:

# echo $VALUES
one~two~~~
# echo $VALUES | awk 'BEGIN {FS="~"} {print NF}'
5
# echo $VALUES | awk 'BEGIN {FS="~+"} {print NF}'
5

Ah, this works:

# echo $VALUES | nawk 'BEGIN {FS="~+"} {print NF-1}'
2

Forget sometimes that in some cases nawk is needed iso awk.

My results are:

echo "one~two~~~" | awk -F~ '{print NF}'
5
echo "one~two~~~" | awk -F~+ ' {print NF}'
3

---------- Post updated at 06:41 PM ---------- Previous update was at 06:39 PM ----------

Try things before you post anything. You ll feel silly after asking :smiley: :smiley:

awk -F"~" '{for(i=1;i<=NF;i++){if($i!="")c++}}{print c}' file

I believe this one is valid to your question :slight_smile: I was in a hurry there ;):wink:

awk '{print gsub(/[^~]+/,x)}'

Leave $0 intact:

awk '{print gsub(/[^~]+/,"&")}'

--

Nope:

$ echo "one~two" | awk 'BEGIN {FS="~+"} {print NF-1}'
1
$ echo "~one~two~" | awk 'BEGIN {FS="~+"} {print NF-1}'
3