LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   showing word list without using "wc" commnad (https://www.linuxquestions.org/questions/linux-newbie-8/showing-word-list-without-using-wc-commnad-862015/)

kakalig2007 02-11-2011 05:11 AM

showing word list without using "wc" commnad
 
Please tell me which command should I use to display count of words from a file, so that the filename is not displayed along with the word count.

Nylex 02-11-2011 05:14 AM

You can just print the first column, by using awk to extract it, e.g.

$ wc -w file | awk '{print $1}'

Aquarius_Girl 02-11-2011 05:18 AM

Putting cksum in Nylex's command produces the byte count though not the word count!?!
Code:

anisha@linux-uitj:~> cksum d.h | awk '{print $1}'
1462178974


Nylex 02-11-2011 05:20 AM

Oops, I misread the question. I thought they just wanted to omit the filename in the wc output, rather than not using wc at all.

ntubski 02-11-2011 10:08 AM

You can send the file on stdin instead of by name:
Code:

~/tmp$ wc -w numbers.txt
10 numbers.txt
~/tmp$ wc -w < numbers.txt
10


w1k0 02-11-2011 10:38 AM

Assuming you have the text file named Hamlet.txt:

Quote:

To be, or not to be, that is the question:
Whether 'tis nobler in the mind to suffer
The Slings and Arrows of outrageous fortune;
Or to take Armes against a Sea of troubles...
you could obtain the list of the words using the command:

cat Hamlet.txt | tr -cs A-Za-z0-9\\047 \\012

Quote:

To
be
or
not
to
be
that
is
the
question
Whether
'tis
nobler
in
the
mind
to
suffer
The
Slings
and
Arrows
of
outrageous
fortune
Or
to
take
Armes
against
a
Sea
of
troubles
to count the words you could use the following command:

cat Hamlet.txt | tr -cs A-Za-z0-9\\047 \\012 | grep '.' -n

Quote:

1:To
2:be
3:or
4:not
5:to
6:be
7:that
8:is
9:the
10:question
11:Whether
12:'tis
13:nobler
14:in
15:the
16:mind
17:to
18:suffer
19:The
20:Slings
21:and
22:Arrows
23:of
24:outrageous
25:fortune
26:Or
27:to
28:take
29:Armes
30:against
31:a
32:Sea
33:of
34:troubles
since you'd like to know just the number of the words use the command:

cat Hamlet.txt | tr -cs A-Za-z0-9\\047 \\012 | grep '.' -n | tail -n 1 | cut -d ':' -f 1

Quote:

34
The more effective method is to use mentioned above wc command but you refuse to use it.


All times are GMT -5. The time now is 11:37 AM.