Linux – How to count the number of rows that have data in a pipe-delimited file

awklinuxtext processing

I have a file like this

1|2345|John|Smith
2|4563||Smith
3|5968||Doe
4|896|Rick|Lawson
5|889||Eddy

How do I count the number of rows that have data in the third column?

Best Answer

awk -F '|' 'length($3) { ++count } END { print count }' < input

On the sample input, it results in:

2

It works by setting the field separator to pipe, then increments count on lines that have a non-empty value in the 3rd field. At the end of the file, it prints the final count.

Related Question