I need to write a script to read each line using a while loop and counting the number of words in each line. So far I can get the total number of lines and text for each in a separate line. I had a problem using the wc-w command to count the number of words for each line and display it. when I put it on the same line as the printf statement gives an inaccurate amount. I need to pass the text tile to the script so that it counts the words, for example: cat file.txt | word_count.sh
any suggestions?
the code:
#!/bin/bash line_num=1 while read line;do printf "line $line_num: $line" ((line_num++)) done
results:
cat imagine.txt | word_counts.sh line1: magine there no countries line2: It isn't hard to do line3: Nothing to kill or die for line4: And no religion too line5: Imagine all the people living life in peace
source share