One way with awk
could be as follows.
We need to realize that the following equality holds:
number of fields = number of delimiters + 1
Note that adding a 0
to the operand in arithmetic comparison, even though not always necessary, is a good practice to inculcate. At least it helps me think about one less thing, for it becomes an auto reflex coding action. Since Awk
does not provide separate operators for arithmetic nd string comparisons, hence coercion is needed to help disambiguate a string from a math operand or rather context.
$ awk -F '[.]' '
NF>m+0 {m=NF}
END {print --m}
' file
4
$ awk '
gsub(/[^.]+/, "") &&
! index(t, $0) { t = $0 }
END { print length(t) }
' file
$ perl -lne '
my $k = tr/.//;
$k > $m and $m = $k;
}{ print $m+0;
' file
The GNU sed
editor can also be used in conjunction with the binary calculator bc
utility. Idea is we keep lines stripped off of all non-dots and the current longest string of pure dots is held in hold. At eof
, we transform the dots into an actionable bc
code to generate the number of those dots.
$ sed -Ee '
s/[^.]+//g;G
/^(.*)..*\n\1$/!ba
s/\n.*//;h;:a
$!d;g;s/./1+/g;s/$/0/
' file | bc -l
tr -dc '\n.' | sort | tail -n1 | wc -m
https://stackoverflow.com/q/8629410 – alecxs Jul 24 '20 at 07:11