I faced an interesting question to count the words from a file where the number of lines were also specified as a command line argument. For example if the input text file had -
Unix is an OS
Linux is the child of Unix
Unix is fun.
End of File
The command to be executed is:
bash test.sh unix.txt 3
where unix.txt
is the test file containing the sentences and 3 is the number of lines whose words are to be counted. The answer would be 13.
I have used the basic wc
commands but none of them would give the correct answer.
So, I tried to use a for loop, but I could not specify how to take only those number of lines.