search and count

Hi,

I would like to seek help regarding searching a pattern on a particular input.

Example input:
"1|trunc(sysdate-1)|substring(pcol)"

I would like to search for "|" and count it.

any help will be much appreciated.

Thanks! :slight_smile:
Newbie

smells like homework, but it educated me on how to waste a few minutes:

me@where[]:
-> typeset -ix pipe_count=$(print "1|trunc(sysdate-1)|substring(pcol)" |tr "|" "\n" |wc -l )
me@where[]:
-> echo $pipe_count - 1 |bc
2

This is of course ksh only, but I'm sure that some of the others out there have other one-liners that could of assistance (awk, perl, whatever...). In fact, you could even boil it down to a one-liner here as well:

echo $(print "1|trunc(sysdate-1)|substring(pcol)" |tr "|" "\n" |wc -l ) - 1 |bc

What it does is break the input into records and then subtract one from the resulting line count. grep -c wouldn't work for you since it's line-based, and tr is fast enough. HTH...

Use awk...

echo "1|trunc(sysdate-1)|substring(pcol)" | awk -F "|" '{print NF-1}'