user1065985
user1065985

Reputation:

What is the unix/linux way to divide the number of lines contained from two different files?

I have a file and I am processing it line by line and producing another file with the result. I want to monitor the percentage of completion. In my case, it is just the number of lines in the new file divide by the number of lines from the input file. A simple example would be:

$ cat infile
unix
is
awesome

$ cat infile | process.sh >> outfile &

Now, if I run my command, I should get 0.33 if process.sh completed the first line.

Any suggestions?

Upvotes: 1

Views: 218

Answers (3)

Juan Tomas
Juan Tomas

Reputation: 5183

TOTAL_LINES=`wc -l infile`
LINES=`wc -l outfile`
PERCENT=`echo "scale=2;${LINES}/${TOTAL_LINES}" | bc | sed -e 's_^\.__'`
echo "${PERCENT} % Complete"

scale=2 means you get two significant digits.

Upvotes: 0

Anthony Battaglia
Anthony Battaglia

Reputation: 2917

You can use bc:

echo "scale=2; $(cat outfile | wc -l) / $(cat infile | wc -l) * 100" | bc

In addition, combine this with watch for updated progress:

watch -d "echo \"scale=2; \$(cat outfile | wc -l) / \$(cat infile | wc -l) * 100\" | bc"

Upvotes: 2

Krzysztof Krasoń
Krzysztof Krasoń

Reputation: 27476

You can use pv for progress (in debian/ubuntu inside package pv):

pv -l -s `wc -l file.txt` file.txt | process.sh

This will use number of lines for progress. Or you can use just the number of bytes:

pv file.txt | process.sh

The above commands will show you the percentage of completion and ETA.

Upvotes: 3

Related Questions