`wc` utility counts newline, word, and byte for file(s) or standard input.

learn more… | top users | synonyms

16
votes
15answers
1k views

How to find line with least characters

I am writing a shell script, using any general UNIX commands. I have to retrieve the line that has the least characters (whitespace included). There can be up to around 20 lines. I know I can use ...
16
votes
4answers
8k views

Why does “ls | wc -l” show the correct number of files in current directory? [duplicate]

Trying to count number of files in current directory, I found ls -1 | wc -l, which means: send the list of files (where every filename is printed in a new line) to the input of wc, where -l will count ...
12
votes
2answers
1k views

Why is wc so slow?

Why is the wc utility so slow? When I run it on a large file, it takes about 20 times longer than md5sum: MyDesktop:/tmp$ dd if=/dev/zero bs=1024k count=1024 of=/tmp/bigfile 1024+0 records in 1024+0 ...
11
votes
8answers
2k views

How do I get the character count of words in a particular column?

I have a CSV file like this: abd,123,egypt,78 cde,456,england,45 How can I get the character count of only the 3rd column words? I can't figure out how to get wc to do this.
9
votes
4answers
17k views

how to count total number of words in a file?

I am looking for a command to count number of all words in a file. For instance if a file is like this, today is a good day then it should print 5, since there are 5 words there.
9
votes
2answers
1k views

Why are wc -m and wc -c different?

As a C programmer, I was surprised to see that wc -c (which count the number of bytes), and wc -m (which counts the number of characters) output very different results for a long, text file of mine. I ...
9
votes
3answers
688 views

Why does “wc -c” print a value of one more with echo?

When running echo abcd | wc -c it returns 5. But the word abcd is only 4 characters long. Is echo printing some special character after the word abcd? And can I prevent echo from printing that? ...
7
votes
6answers
183 views

How to efficiently split up a large text file wihout splitting multiline records?

I have a big text file (~50Gb when gz'ed). The file contains 4*N lines or N records; that is every record consists of 4 lines. I would like to split this file into 4 smaller files each sized roughly ...
6
votes
4answers
1k views

How do I get a single total of lines with `wc -l`?

I've added a git alias to give me the line counts of specific files in my history: [alias] lines = !lc() { git ls-files -z ${1} | xargs -0 wc -l; }; lc However, wc -l is reporting multiple totals, ...
5
votes
3answers
2k views

How does word count work for newline (-l)? Number of newlines or lines

I wonder how does wc work? Most times it seems to return the number of lines, but sometimes it seems to be newlines? In the man page its newlines. But: [jiewmeng@JM textFiles]$ echo -e "\n\n" | wc - ...
5
votes
4answers
176 views

Count lines of non-terminating input

Is there any way to count the lines of a non-terminating input source? For example, I want to run the following for a period of time to count the number of requests: ngrep -W byline port 80 and dst ...
4
votes
3answers
278 views

Copy a specific percentage of each file in a directory to a new file

For example, we have N files (file1, file2, file3 ...) We need first 20% of them, the result directory should be like (file1_20, file2_20, file3_20 ...). I was thinking use wc to get the lines of ...
4
votes
2answers
1k views

Number of lines outputted by ls

When ls is called, it outputs all the files/directories in the current directory, attempting to fit as many as possible on each line. Why is it that when passed to wc -l, it outputs the number of ...
4
votes
2answers
767 views

Counting lines without text in a file

I am using Ubuntu 13.10 for a while now, programming in C++ and Python. I have some question that I can solve by making a program, but maybe there are easier ways, so here is the first question. I ...
4
votes
4answers
912 views

Count lines matching pattern and matching previous line

I want to count the number of times that a role is successfully deleted. The problem is that one line of the log file will show that the script is about to delete the role: Prepare to remove role X ...
4
votes
2answers
2k views

Character count in Unix wc command

When I give wc command on a file with contents given below, it gives the number of characters as 30. Does it include the end of file character? Since including the space and newline there are only 29 ...
4
votes
3answers
2k views

Get correct number of lines in diff output

I want to get the correct number of lines in the output of diff(specifically with -y and --suppress-common-lines options). Using a simple wc -l does not work, because if both files end without a ...
4
votes
1answer
338 views

command like wc but with tee behaviour

I want to backup a database using psql and COPY command. here is my script: psql "user=${USERNAME} host=${HOSTNAME} dbname=${DBNAME} password=${PASSWORD}" -c \ "COPY (SELECT * FROM tbl) ORDER BY id ...
4
votes
2answers
913 views

How to count the number of lines in a UTF-16LE/CR-LF/BOM file?

The immediate thought is wc, but then the next not-so-immediate thought is... Is *nix's wc purely for *nix line endings \x0a?... It seems so. I've semi-wangled my way around it, but I feel there ...
4
votes
3answers
3k views

Running external commands in vim

It seems that if I run the following inside the vim: :w !wc -w I get the word count of the file. But I don't understand the syntax. How does this work and how would I specify that I want the ...
4
votes
3answers
221 views

Time required to do pipe output to head/tail [duplicate]

There are lots of txt files in a directory. If I do time wc -l *.txt | head it takes real 0m0.032s user 0m0.020s sys 0m0.008s If I do time wc -l *.txt | tail it takes real 0m0.156s ...
4
votes
6answers
2k views

How can I split a large text file into chunks of 500 words or so?

I know there's the command split which can split a file into several chunks based on file size, and then there's the command wc which can count words. I just don't know how to use the two together.
3
votes
4answers
3k views

How to use wc and piping to find how many files and directories are in a certain directory?

How can I use word counter and piping to count how many files or directories are in /usr/bin directory?
3
votes
1answer
5k views

The simplest method to count lines matching specific patterns, including '0' if line is not found?

I have very big logs (several gigabytes per day), that can (but do not need to) contain specific lines. I have to count the number of occurences of every one of these lines on a daily basis. I have a ...
3
votes
5answers
321 views

List files with line count and date

I want to be able to list files showing the number of lines the each file has and the date. I can happily get the line count using wc -l *. Not a problem. I can get the date using ls -l. Is there a ...
3
votes
4answers
363 views

How to display the number of lines, words and characters in separate lines?

I want to display the number of lines, words and characters, in a file, in separate lines? I don't know anymore than using wc filename for this.
3
votes
1answer
86 views

wc command issue

I have only one row in my file. When I execute wc -c file1.txt the result is 200, while the output of wc -L file1.txt is 198. What explains the difference of 2?
3
votes
2answers
70 views

I want to reboot the system once a search had been done to see that no-one is logged in

I'm working on a user menu so that when a user logs in all they get is this menu; on this menu is a selection of reboot option. I want one of the options to be reboot the system if no one is logged ...
3
votes
2answers
50 views

How can I count the number of whitespace characters in a file?

I wanted to count the number of whitespace characters in a file. The best I could come up with was: tr -cd [:space:] < my_file | wc Is there a neater way?
3
votes
1answer
216 views

Split input file into X parts of size total lines / X

Currently, my bash script splits by number of lines. However, I'd like to split a file into X pieces, each of those having total lines equal to the file length divided by X. The script is run as ...
3
votes
1answer
341 views

Run wc and display only the word count

I have been trying to create a file called ~/dictwords.txt, which contains the number of words found in the file /usr/share/dict/words. I already created the file then I removed it because I did it ...
3
votes
1answer
199 views

Inconsistent input redirection behavior

I wrote a 10000010-byte file (99010 rows of 100 consecutive a's) named big for experimentation purposes. I then copied the entire text into my clipboard, ran wc, and pasted it into stdin. Next, I did ...
2
votes
3answers
2k views

Count number of characters per listed filename

Given the following ls result: $ ls Desktop Documents Downloads Music Pictures Public Templates Videos This will give the total number of characters for all files: $ ls | wc -m 67 But how ...
2
votes
2answers
42 views

Extra space with counted line number?

I count the number of lines of my file with this command on OSX: nl=$(wc -l < ~/myfile.txt) Say, nl turns out to be 100. Now, I wish to use the result nl in another command, but weirdly, echo ...
2
votes
1answer
310 views

Counting the numbers of lines of data as it comes in? [duplicate]

I am currently connected to the Twitter Stream, and I would like to count the number of lines of data as they come in. I know I can do wc-l to just count the lines, but I want it to be auto-updating. ...
2
votes
3answers
1k views

How to make wc interpret standard in as a file list

I know that there are other ways to go about this, but I'm looking to be able to make wc interpret stdin as a file name or list of file names. For example, ls JP*/std* | wc would work the same as ...
2
votes
2answers
48 views

grep with count of individual patterns

I have a file with MAC addresses (1/line with : as separator, sorted) and I need to find out how many times each of these MAC addresses appears in the file. I modified this: How to count occurrences ...
2
votes
2answers
86 views

How to append word count of file to file without adding the total

I am running a bash script where I am appending the line count of the arguments, which are files, to another file. It does it correctly, but I keep getting a line that says "total X", X being the sum ...
2
votes
1answer
355 views

Return count of matches with find/replace using sed

I'm trying to get a count of the number of replaced strings with find and sed. What I'm starting with: # find . -name \*.php -exec sed -i -e "s|finddddd|replaceeeee|g" {} \; This replaces four ...
2
votes
2answers
1k views

Why does wc -l tell me that this non-empty file has 0 lines?

xclip/xsel are utilities that allow you to pipe clipboard output to the shell on Ubuntu. Unfortunately, they seem to fail in combination with the wc command. Try this. Open a new file and type: ...
1
vote
4answers
3k views

Count tabs per line in text file with utils

I have a tab-tabulated file. I would like to check if every line has the same number of tabs. For first step, I'd like to print the number of tabs for each individual line. I've tried grep -o '\t' ...
1
vote
4answers
138 views

get file size and line count at the same time

I'm looking for a way to list all files in a directory, with both their size, and a line count. Right now I'm using stat -c \"%s %n\" /directory/* to get file names and sizes, and I know I can use ...
1
vote
2answers
239 views

How many lines of code are in this file?

I'm trying to figure out how many lines of code I have for files that end in *.pp find . -name *.pp -type f | wc -l 39 I know that I have 39 files, how can I figure out how many lines of code ...
1
vote
2answers
415 views

Display the number of bytes, words and lines in a file

I want to create a shell script df. As, I want to type in df name_of_file and then I can get the contents of the file. If the file name is in the directory, I want it to show its contents. If not, I ...
1
vote
3answers
318 views

Print folders names character count

I am trying to use find . -type d -print | wc -m where -m or --chars would print the character counts of the folder names. Take following screen shot as example, the character count for tempFolder + ...
1
vote
1answer
554 views

Speeding a find rm command with test through parallelization

I want to recursively delete all files in directories and subdirectories with number of lines less than 10, and am currently using the following command find . -type f -name "*.txt" | while read; do ...
1
vote
2answers
41 views

How to print few lines from middle of a file using unix shell commands? [closed]

My file containing the following lines, $BQ { VOL @home } database daba relation tcdeatid opendb clear .lruno := 72 <-- (This line lruno. := 72 has 10 times ...
1
vote
1answer
53 views

wc -m command in raspberry pi adding an extra number

I am writing a program on the pi using wc -m command to count the amount of letters in a string, however, it counts the letters but adds one extra. This is what I currently have. echo "enter a word" ...
1
vote
2answers
29 views

Count and list down + advanced features

I have a few questions: I have listed the number of lines in the files in /group/book/four/word, sorted by the number of lines they contain using $ wc -l /group/book/four/word/*|sort -n ...and ...
0
votes
3answers
211 views

Count words: what will use less CPU, wc or a perl script?

My objective is to count the words in a file while using the minimum possible CPU. I could use the wc command or write a simple perl script for this, which of the two options will be less CPU ...