To find number of char occur

To find out number of "|" symbol is available in file:

Input:

a|b|c|d|z

Ouput:

4

I am using below set of commands,It is working... Anybody have anyother solution using sed / awk.

    cnt=`wc -c <1.txt`
    cnt1=`tr -d "|" <1.txt >c.dat`
    cnt2=`wc -c <c.dat`
    outp=`expr $cnt - $cnt2`

Thanks in advance.

% print 'a|b|c|d|z' | awk -F\| '$0=NF-1'
4

For 0 occurrences you won't get any output.

It is really great. Can you please explain the working follow of above awk command.

Thanks a lot for your help.

It sets the pipe as a input field separator and then sets the internal variable $0 (the current input record) to the number of fields - 1.

another way in Perl.

perl -pe '$_ = s#\|#\|#g'  < input_file 
4

Or:

% perl -le'print shift=~tr/|//' 'a|b|c|d|z'
4

or,

echo 'a|b|c|d|z' | tr -dc '|' | wc -c

With zsh:

% print ${#${:-'a|b|c|d|z'}//[\!|]/}
4

bash (and zsh[1], ksh93):

$ var='a|b|c|d|z' c=${var//[!|]/};echo ${#c}
4

[1] The bang should be escaped.

Another one:

awk '{print gsub("\|","")}' file

Great Franklin.

and another one

echo "a|b|c|d|z"| grep -o "|" | wc -l

With ruby:

ruby -e'
  puts "a|b|c|d|z".count("|")
  '

And some more Perl -

$ 
$ echo "a|b|c|d|z" | perl -lne 's/[^|]//g && print length'
4
$ 
$ echo "a|b|c|d|z" | perl -F"[^|]" -plae '$_=$#F'
4
$
$ echo "a|b|c|d|z" | perl -ple 's/[^|]//g; $_=length'
4
$
$ echo "a|b|c|d|z" | perl -plne '$_=()=$_=~/\|/g;'
4
$ 

tyler_durden

---------- Post updated at 10:38 PM ---------- Previous update was at 10:11 PM ----------

Btw, it ain't fair to leave out Python... :wink:

$ 
$ echo "a|b|c|d|z" | python -c "import sys; print sys.stdin.read().count('|'),"
4
$ 

tyler_durden

echo 'a|b|c|d|z' | tr -dc 'a' | wc -c
2

can any body explain why it's coming 2 , also strange to see if we go for d in place of a

its working for me.

/home-dev/->echo 'a|b|c|d|z' | tr -dc 'a' | wc -c
1
/home-dev/->echo 'a|b|c|d|z' | tr -dc 'd' | wc -c
1
/home-dev/->

which OS do you have?

i'm doing in cygwin

Yet another Perl version:

% echo "a|b|c|d|z"|perl '-nEsay~~@{[/\|/g]}' 
4

---------- Post updated at 08:37 AM ---------- Previous update was at 08:34 AM ----------

I cannot reproduce this:

% uname 
CYGWIN_NT-5.1
% echo 'a|b|c|d|z' | tr -dc a | wc -c
1
% echo 'a|b|c|d|z' | tr -dc d | wc -c
1

@Radoulov

% uname 
CYGWIN_NT-5.1
% echo 'a|b|c|d|z' | tr -dc a | wc -c
1
% echo 'a|b|c|d|z' | tr -dc d | wc -c
1

Yes , you are correct i got the exact result .what you are getting.Actually i placed print in place of echo so that i got the erroneous result, As print command is not found in CYGWIN_NT-5.1.

Thank & Regard's
posix

Just to clarify that print is not OS specific, but shell specific:
some shells (ksh (and ksh clones), zsh) provide the print builtin command:

$ for sh in bash zsh ksh; do
  eval "$sh -c 'echo \$0 \$(type print)'"
done> >
bash: type: print: not found
bash
zsh print is a shell builtin
ksh print is a shell builtin
echo 'a|b|c|d|z' | sed 's/[^[:punct:][:punct:]]*/\n/g'|sed '/./!d' | wc -l