Count number of lines of output from previous program

I’m trying to count the number of lines of output a certain program produces. The problem is, the program takes a long time to run, and I want to display the output to the user. Is there a way to count the number of lines the last command outputted?

I could do program | wc -l but that wouldn’t show the output to the user. So as far as I know, I have to do program; program | wc -l – but the program takes at least a minute to run, so I don’t want to have to do it more than once just to show a line count at the bottom.

EDIT:

  • Is there a way of showing the output as it happens (line by line) and then returning a count at the end?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can use tee to split the output stream sending one copy to wc and the other copy to STDOUT like normal.

program | tee >(wc -l)

The >(cmd) is bash syntax which means run cmd and replace the >(cmd) bit with the path to (a named pipe connected to) that program’s STDIN.

Method 2

One option is to use awk, which can do the counting and print to stdout.

program | awk '{ print } END { print NR }'

In awk, NR is the current line number. You can accomplish the same with perl:

program | perl -pe 'END {print "$.n"}'

Or sed:

program | sed -n 'p;$='

Method 3

my favorite option:

program | grep "" -c

Method 4

You can clone stdout on stderr.

program | tee /dev/stderr | wc -l

That way, program‘s stdout is piped to tee to be written to stderr, which is printed on the console. tee also writes the data piped to it to its stdout, which is piped to wc.

Method 5

This might be late. But I would just address your follow up question on how to catch the counted number in a variable.

This is what you want YOUR_VAR=$(PROGRAM | tee /dev/stderr | wc -l).

We take advantage of tee generating two streams here and direct one to /dev/stderr, which would appear on you screen, and the other to wc -l, which would report the number of lines.

Method 6

tail -f /var/log/squid/access.log | ( c=0; pl() { echo $c; c=0; }; trap pl SIGHUP; while read a; do (( c=c+1 )); done ) & ( trap 'kill $! ; exit' SIGINT; trap '' SIGHUP; while true; do kill -HUP $! ; sleep 1; done)


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x