I have tried:
grep -c '\|' *.*
But it didn’t work, since it gives an incorrect count of consecutive pipes.
How can I accomplish this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Another option, using Perl, is:
In non-one-liner format that’s:
The
while(<>){line is Perl magic for reading lines from files on the command or fromSTDIN. You get used to it after a while. The line itself goes into a variable called$_, which is the default parameter for many Perl commands. For instancetr, which works quite a bit liketr(1), defaults to operating on$_. I’m putting my results in a global variable called$c. (In a full program, it’s best to declare it a lexical variable withmy $c = 0;outside the loop.) The+=operator adds the result of thetrcommand (the number of pipe characters in this case) to the current value of$c.Just using
tr(1)is clearly a simpler option. 😉Using
*.*is a DOSism that you don’t likely want to use on a UNIX-like platform.Using single quotes to avoid having the shell interpret the pipe character reads a bit better. For instance, I tested my answer with: