Alvin Alvin - 1 year ago 100
Linux Question

Counting lines starting with a symbol

There are many lines contain > symbol in a file. How can I count the total number of > symbol in a file ? I have tried

and it did not work.

Answer Source

You can use GNU grep together with wc

grep -o '>' file.txt | wc -l

grep -o prints every match on a separate line. wc counts the lines.

Btw, it's not 100% clear in your question if the > can appear only at the start of a line. If you just want to count the lines that start with a > you can use the following grep command:

grep -c '^[[:space:]]*>' file.txt

^ matches the beginning of the line, [[:space:]]* allows for zero ore more space characters in front of the >, just in case.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download