#!/bin/bash -
# @(#) s1 Demonstrate count of strings with awk, field separator.
echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) awk
set -o nounset
FILE=${1-data1}
echo
echo " Data file $FILE:"
cat $FILE
echo
echo " Results:"
awk -F",5," '
BEGIN { t = 0 }
{ t += NF }
END {print t}
' $FILE
Producing:
% ./s1
(Versions displayed with local utility "version")
Linux 2.6.11-x1
GNU bash 2.05b.0
GNU Awk 3.1.4
Data file data1:
1,1,2,5,5,5,6,5,4,5,7
Results:
5
In many RE implementations, one cannot have over-lapping matched sections of strings, so the result noted makes sense in that context.
I was surprised that my solution on post # 9 worked, since I would have expected the same behavior with a field split based on a regular expression as separator.
Perhaps the explanation is that for the gsub, part of the string is now replaced by the empty string which makes the next ",5," in the original no longer present, but rather "5,", causing the next one to be considered. For the field split, there is no replacement ... cheers, drl