Feras Feras - 1 year ago 73
Linux Question

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

$ cat infile | process.sh >> outfile &

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

Any suggestions?

Answer Source

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"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download