Word Count (WC) command


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Word Count (WC) command
# 1  
Old 01-16-2009
Network Word Count (WC) command

I have created a file "pat".This file contains the text "abcdefghi". Now i want to count the characters in this file "pat". I gave wc -c | pat and it returns me exact count "9". Now when i tried like "echo pat | wc -m" It returns '4'. as for as i understand this command "echo pat | wc -m" should work similiar to "wc -m". I might be wrong. Can some one please clarify my doubt?
# 2  
Old 01-16-2009
Code:
echo pat | wc -m

This counts the string "pat", why its give 4 and not 3 im not sure.

Code:
cat pat | wc -m

would count the chars in the file pat, gives 10 on my pc
# 3  
Old 01-16-2009
echo pat | wc -m returns 4 instead 3. Can you please explain how?
# 4  
Old 01-16-2009
The use of cat is redundant, this is sufficient:

Code:
wc -m < pat

Regards
# 5  
Old 01-16-2009
Quote:
Originally Posted by mdali_vl
echo pat | wc -m returns 4 instead 3. Can you please explain how?

echo pat
outputs "p", then "a", then "t", and finally it outputs a newline character.
# 6  
Old 01-16-2009
Code:
The echo has added a linefeed character which is included in the count.
Here "fgh" is just a text string not the name of a file.

echo "fgh" | wc -l

4

If you use echo without the linefeed character the count goes down.

echo "fgh\c" | wc -l

3


Where your file is called "pat" and contains a 9 character string "abcdefghi" ending with a linefeed (i.e. a normal text file record).

cat pat | wc -c

10

If the file called "pat" contains two records each of which is the 9 character string "abcdefghi" :

cat pat | wc -c

20

# 7  
Old 01-16-2009
Quote:
Originally Posted by mdali_vl
echo pat | wc -m returns 4 instead 3. Can you please explain how?
Echo prints a newline character after the line, to avoid this you can use the -n option:

Code:
echo -n "pat" | wc -m

Regards
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get count of multiple word in single command

Hello Experts, I have a log file that contains 4 different type of exception : 1- Exception 2- Fatal 3- Error 4- Exec My requirement is to find count of each type of exception, i tried using combination of -E and -C but that doesn't seems to be working : grep -ec 'Exception' -ec... (4 Replies)
Discussion started by: mukulverma2408
4 Replies

2. UNIX for Beginners Questions & Answers

UNIX script to check word count of each word in file

I am trying to figure out to find word count of each word from my file sample file hi how are you hi are you ok sample out put hi 1 how 1 are 1 you 1 hi 1 are 1 you 1 ok 1 wc -l filename is not helping , i think we will have to split the lines and count and then print and also... (4 Replies)
Discussion started by: mirwasim
4 Replies

3. Shell Programming and Scripting

~~Unix command to count a particular word in the whole directory .~~

Hi , i'm trying to count a particular word occurance in a whole directory..is this possible :wall: say for example there is a directory with 100 files which and all the file may have the word 'aaa' in it ...how would i count the number of 'aaa' in those whole 100 files in a directory ? ... (10 Replies)
Discussion started by: Rabbitsfoot
10 Replies

4. Shell Programming and Scripting

if, word count

Hi, I need to count the lines of a file stack.html and if the amount lines i want to do something. At this moment, I have if ; then ... This is not working. Any ideas? Thanks! (3 Replies)
Discussion started by: azertyazerty
3 Replies

5. UNIX for Advanced & Expert Users

Word count

Script that lists all words used in one or more files and displays their count (pattern /\W+/ to split the lines of the input file into words can b used).. It should display list in format word:count...gets Filename as an cmd line argument! eg: $perl test doc (where doc is d file we are going to... (4 Replies)
Discussion started by: aadi_uni
4 Replies

6. Shell Programming and Scripting

Word count of lines ending with certain word

Hi all, I am trying to write a command that can help me count the number of lines in the /etc/passwd file ending in bash. I have read through other threads but am yet to find one indicating how to locate a specifc word at the end of a line. I know i will need to use the wc command but when i... (8 Replies)
Discussion started by: warlock129
8 Replies

7. Fedora

word count wc

could someone explain this please. echo aaaa|wc -c 5 echo aaaa|wc -m 5 But I'd expect the count to be 4 Its SunOS 5.8 Thanks in Advance. (5 Replies)
Discussion started by: chaandana
5 Replies

8. UNIX for Dummies Questions & Answers

Word Count

Hi All, Please let me know how to get the count of a particular word in a file. Example. I am looking for count of word 'result' in a file abc.xml. Thanks, Shankar (10 Replies)
Discussion started by: s_chowhan
10 Replies

9. Shell Programming and Scripting

specified word count

hi iam trying to do a specified word count on file called text i have a few ideas but don't get the result i want do any one have a idea please help i have this at the moment cat text echo "Please enter the word you are looking for:" read string echo "the word < $string > occurs in... (5 Replies)
Discussion started by: bhaviknp
5 Replies

10. UNIX for Dummies Questions & Answers

count word

hi, given a file i need to get the first line and secodn line and count each of the line whether the length of first line and second line is the same i don;t know how to get the length of the line...seems like use 'wc' cannot do it... please advice (1 Reply)
Discussion started by: ariuscy
1 Replies
Login or Register to Ask a Question