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
216
votes
10 answers

How to get "wc -l" to print just the number of lines without file name?

wc -l file.txt outputs number of lines and file name. I need just the number itself (not the file name). I can do this wc -l file.txt | awk '{print $1}' But maybe there is a better way?
pogibas
  • 27,303
  • 19
  • 84
  • 117
143
votes
19 answers

Get just the integer from wc in bash

Is there a way to get the integer that wc returns in bash? Basically I want to write the line numbers and word counts to the screen after the file name. output: filename linecount wordcount Here is what I have so far: files=\`ls` for f in…
Jordan
  • 4,928
  • 4
  • 26
  • 39
93
votes
11 answers

How to count lines of code including sub-directories

Suppose I want to count the lines of code in a project. If all of the files are in the same directory I can execute: cat * | wc -l However, if there are sub-directories, this doesn't work. For this to work cat would have to have a recursive mode. I…
speciousfool
  • 2,620
  • 5
  • 28
  • 33
82
votes
6 answers

bash echo number of lines of file given in a bash variable without the file name

I have the following three constructs in a bash script: NUMOFLINES=$(wc -l $JAVA_TAGS_FILE) echo $NUMOFLINES" lines" echo $(wc -l $JAVA_TAGS_FILE)" lines" echo "$(wc -l $JAVA_TAGS_FILE) lines" And they both produce identical output when the…
Marcus Junius Brutus
  • 26,087
  • 41
  • 189
  • 331
44
votes
6 answers

Unix Command to get the count of lines in a csv file

Hi I am new to UNIX and I have to get the count of lines from incoming csv files. I have used the following command to get the count. wc -l filename.csv Consider files coming with 1 record iam getting some files with * at the start and for those…
Devoloper250
  • 753
  • 2
  • 8
  • 12
36
votes
7 answers

results of wc as variables

I would like to use the lines coming from 'wc' as variables. For example: echo 'foo bar' > file.txt echo 'blah blah blah' >> file.txt wc file.txt 2 5 23 file.txt I would like to have something like $lines, $words and $characters associated to the…
719016
  • 9,922
  • 20
  • 85
  • 158
30
votes
6 answers

How to count lines fast?

I tried unxutils' wc -l but it crashed for 1GB files. I tried this C# code long count = 0; using (StreamReader r = new StreamReader(f)) { string line; while ((line = r.ReadLine()) != null) { count++; } } return count; It…
Jader Dias
  • 88,211
  • 155
  • 421
  • 625
30
votes
8 answers

Use find, wc, and sed to count lines

I was trying to use sed to count all the lines based on a particular extension. find -name '*.m' -exec wc -l {} \; | sed ... I was trying to do the following, how would I include sed in this particular line to get the totals.
Berlin Brown
  • 11,504
  • 37
  • 135
  • 203
21
votes
2 answers

The wc -l gives wrong result

I got wrong result from the wc -l command. After a long :( checking a found the core of the problem, here is the simulation: $ echo "line with end" > file $ echo -n "line without end" >>file $ wc -l file 1 file here are two lines, but…
novacik
  • 1,497
  • 1
  • 9
  • 19
13
votes
4 answers

Count the number of characters, words and lines in PowerShell

In Linux we have the "wc" command which allows us to count the number of characters, words and lines in a file. But do we have a similar cmdlet in PowerShell. The Measure-Object cmdlet I tried could only count the number of lines but not the…
Mufa Sam
  • 165
  • 1
  • 2
  • 6
12
votes
4 answers

Bash: Find file with max lines count

This is my try to do it Find all *.java files find . -name '*.java' Count lines wc -l Delete last line sed '$d' Use AWK to find max lines-count in wc output awk 'max=="" || data=="" || $1 > max {max=$1 ; data=$2} END{ print max " " data}' then…
Marek Sebera
  • 39,650
  • 37
  • 158
  • 244
10
votes
7 answers

Counting characters, words, length of the words and total length in a sentence

I have to write a script that takes a sentence and prints the word count, character count (excluding the spaces), length of each word and the length. I know that there exist wc -m to counter number of characters in the word, but how to make use of…
mydreamadsl
  • 568
  • 4
  • 16
  • 25
9
votes
2 answers

How to find the total lines of csv files in a directory on Linux?

Working with huge CSV files for data analytics, we commonly need to know the row count of all csv files located in a particular folder. But how to do it with just only one command in Linux?
Cesar Augusto Nogueira
  • 1,911
  • 1
  • 14
  • 14
9
votes
2 answers

Why is the "wc" command saying that I've got only one line in a file while in fact there are really two?

Take a look at this example please: $ cat < demo man car$ $ $ od -x < demo 0000000 616d 0a6e 6163 0072 0000007 $ $ wc < demo 1 2 7 As you can see, I've got there 3 characters (man: 6d 61 6e) followed by a newline (\n: 0a) and then another…
user4087080
8
votes
1 answer

bash "wc -l" command output differs if call or through tee

When I issued two equivalent commands in Bash I got different output (from "wc -l" command), see below: root@devel:~# ls /usr/bin -lha | tee >(wc -l) >(head) > /dev/null total 76M drwxr-xr-x 2 root root 20K Nov 11 18:58 . drwxr-xr-x 10 root…
urusai_na
  • 393
  • 4
  • 11
1
2 3
23 24