count number of words and lines from stdin
You use wc
(word count) with the -w
option to count words in a file or -l
for lines.
$ cat file.txt
this file has 5 words.
$ wc -w file.txt # Print number of words in file.txt
5 file.txt
$ wc -l file.txt # Print number of lines in file.txt
1 file.txt
$ wc file.txt # No option, print line, words, chars in file.txt
1 5 23 file.txt
Other options to wc
:
-c, --bytes print the byte counts
-m, --chars print the character counts
-l, --lines print the newline counts
-L, --max-line-length print the length of the longest line
user1832605
Updated on August 25, 2022Comments
-
user1832605 over 1 year
How can i write a program using STANDARD UNIX UTILITIES that will read data from standard input one character at a time and out the results to standard output. I know that it runs similar to what a C program in this case. I was told that this could be done in one line of code but never done Unix Pipeline Programming so i am just curious. The purpose of the program is to read data from standard input and count the number of words an lines and out in standard output the total number of words and lines
I came up with the following code but i am unsure:
tr A-Z a-z < file | tr -sc a-z | sort uniq -c wc '\n'
Any thoughts or suggestions on how i can get what i want?