Questions tagged [wc]

`wc` is a standard POSIX command that counts words, lines, and characters.

The wc utility reads one more files (or the standard input) and writes the number of lines, words, and characters to the standard output. If more than one file is given, then the total number of lines, words, and characters across all inputs will also be printed. There are also switches for controlling what parts should be counted.

Some versions of wc can differentiate between characters (which may be multi-byte depending on the encoding) and bytes.

References:

350 questions
8
votes
1 answer

"grep -c" versus "wc -l"

I am processing a number of large text files, ie. converting them all from one format to another. There are some small differences in the original formats of the files, but - with a bit of pre-processing in a few cases - they are mostly being…
John W
  • 81
  • 1
  • 3
7
votes
1 answer

Check if WC Command output is greather than in BASH

I need to check if wc command output is greather than a variable. Here's my code: if test wc -w $i -gt $num then echo "too great" fi If the current file $i contains more words than the $num variable i print "too great". I already tried all but…
7
votes
5 answers

Word count for sequence length is wrong

I have a fasta file that looks like this : >0011…
EvenStar69
  • 243
  • 3
  • 15
7
votes
2 answers

Unexpected leading spaces while using wc -l command

I am trying to execute the below command - but the output has some leading space introduced. ls -lrt | wc -l 29 echo $SHELL /bin/bash When I run the same command on a different machine,the output is as expected. ls -lrt | wc -l 183 echo…
Soumya
  • 885
  • 3
  • 14
  • 29
7
votes
1 answer

Bash: output line count from wc in human readable format

Is that possible? Doing wc the straight forward way I have to spend some mental energy to see that the file contains more than 40 million lines: $ wc -l 20150210.txt 45614736 20150210.txt I searched around and numfmt showed up, but that is…
luffe
  • 1,588
  • 3
  • 21
  • 32
7
votes
4 answers

Correctly count number of lines a bash variable

I need to count the number of lines of a given variable. For example I need to find how many lines VAR has, where VAR=$(git log -n 10 --format="%s"). I tried with echo "$VAR" | wc -l), which indeed works, but if VAR is empty, is prints 1, which is…
linkyndy
  • 17,038
  • 20
  • 114
  • 194
6
votes
2 answers

"OSError: [Errno 22] Invalid argument" on Windows with print() and output piped

I've come across some (to me) weird behaviour when piping the output of a Python script into wc with invalid arguments. λ python test.py Hello! λ python test.py | wc -li wc: unknown option -- i Try 'wc --help' for more information. Exception ignored…
Niklas R
  • 16,299
  • 28
  • 108
  • 203
6
votes
5 answers

WC on OSX - Return includes spaces

When I run the word count command in OSX terminal like wc -c file.txt I get the below answer that includes spaces padded before the answer. Does anyone know why this happens, or how I can prevent it? 18000 file.txt I would expect to get: 18000…
ATS
  • 174
  • 1
  • 10
6
votes
4 answers

Easily count words in a list of files in a folder after grep -v command

I have been trying to make the scripts I write simpler and simpler. There are numerous ways to write get the word count of all files in a folder, or even all files of subdirectories of a folder. For instance, I could write wc */* and I might get…
Vincent Laufer
  • 705
  • 10
  • 26
5
votes
1 answer

how to use shell to count Chinese characters in file encoded in UTF-8

cat doc.txt and the following characters will show: 你好 Hello! 这是中文。This is a Chinese doc. I can use the command wc -w doc.txt but it will show: 8 doc.txt this command take characters 你好 and 这是中文 both as a single word, while in fact 你好 are…
Arron Cao
  • 416
  • 2
  • 9
5
votes
6 answers

Bash: Native way to check if an entry is one line?

I have a find script that automatically opens a file if just one file is found. The way I currently handle it is doing a word count on the number of lines of the search results. Is there an easier way to do this? if [ "$( cat "$temp" | wc -l | xargs…
redolent
  • 4,159
  • 5
  • 37
  • 47
5
votes
2 answers

Counting characters in a UTF-8 file

wc -c appears to only do a dumb bytecount, not interpret actual characters with regard for encoding. How can I get the actual character count?
user2958725
  • 1,355
  • 3
  • 12
  • 16
5
votes
3 answers

How does "wc -w < file.txt" work?

I was trying to get only the number of words in a file using wc. wc -w file.txt gives me that plus the file name. I don't want the file name. So, I saw that wc -w < file.txt works. I don't understand how this command works. I cannot even add a…
Steam
  • 9,368
  • 27
  • 83
  • 122
4
votes
1 answer

Why do wc -w and Python's len(text.split()) give a different result?

In which circumstances would the Unix command line utility 'wc' and Python's len(text.split()) give a different result? A bit of context, although it shouldn't be relevant because the only thing we are doing here is counting words/tokens (ie. sets…
nohamk
  • 325
  • 2
  • 11
4
votes
1 answer

MacOS wc (wordcount) counts wrong words with UTF-8 character Å

When using wc on the string Ås (swedish letter capital Å) I get wordcount 2 when I expected wordcount 1. Counting the words Å, sÅ gives 1, which feels correct. $ echo sÅ | wc 1 1 4 $ echo Å | wc 1 1 3 Counting…
Mikael Roos
  • 285
  • 3
  • 15
1
2
3
23 24