Count character in one line

Please check the attachment for the example.
Purpose: count how many "|" character in one line and also display the line number.

expect result:

Line 1 : there are 473 "|" characters
Line 2 : there are 473 "|" characters

I have tried to use awk to count it, it's ok when the statistic character is less than 199.

awk -F'|' '{print NR,FS,NF-1}' 1111.txt
awk: Line ||||00000000000033||4 cannot have more than 199 fields.
 The input line number is 1. The file is 1111.txt.
 The source line number is 1.

Thanks for your advice.

Try this...

awk '{print "Count : "gsub(/\|/,"1")}' infile

--ahamed

if his awk can't even have 200 separators in a line, it sure won't have gsub.

Please explain what your system is so we know what tools we can advise you to use.

Try /usr/xpg4/bin/awk instead of plain awk if you're on solaris.

The Perl way.

$ perl -ne 'print "Line $. has ".tr/|//." pipes.\n"' inputfile
Line 1 has 473 pipes.
Line 2 has 473 pipes.
 
nawk -F\| '{printf("Line %s : there are \"%s\" Characters\n",NR,NF)}' inputfile.txt

If you want to find out any the lines which doesnt have 473 fields, then use the below

 
nawk -F\| 'NF!=473{print NR}' inputfile.txt

NF-1 will give the actual data.

--ahamed

Thanks for your comments. the environment as follows:

HPUX 11
Bourne Shell, no bash
AWK, no NAWK

---------- Post updated at 01:14 AM ---------- Previous update was at 01:01 AM ----------

It's cool. Thanks! I am not good at PERL :frowning:
If I want to summarize the strings "abc" in one line(also more than 200 times). The script of PERL should be ?

$ cat inputfile
abc,pqr,nye,qoew,aslkjgb,abc,alskjdf,alsdkjf,abc
abc,pqr,nye,qoew,aslkjgb,abc,alskjdf,alsdkjf,abc,abc
$
$ perl -ne 'print "Line $. has ".s/abc//g." occurrence(s) of abc\n"' inputfile
Line 1 has 3 occurrence(s) of abc
Line 2 has 4 occurrence(s) of abc

---------- Post updated at 12:51 ---------- Previous update was at 12:45 ----------

It's surprising that awk one-liners from post #2, #5 aren't working on your system!

There's some very old versions of awk out there.

What's more surprising is that they didn't start working once he started trying nawk, gawk, and the like. Most systems have one or the other.