1

I want to get only the number of lines in a file: so I do:

$wc -l countlines.py 
   9 countlines.py

I do not want the filename, so I tried

$wc -l countlines.py | cut -d ' ' -f1

but this just echo empty line.

I just want number 9 to be printed

eagertoLearn
  • 9,772
  • 23
  • 80
  • 122
  • @UliKöhler: I checked the link you posted as duplicate, which is `wc -l countlines.py | cut -f1 -d' '`, but it did not work. – eagertoLearn Jan 14 '14 at 23:57
  • 1
    Indeed the first answer doesnt solve your issue, but I believe a modified version of the second answer (which has the highest score) does. See my post below. – Uli Köhler Jan 15 '14 at 00:01
  • Note: The first answer at the other post seems to work any time the number of lines/words is > 999 – Uli Köhler Jan 15 '14 at 00:02
  • @UliKöhler: Thanks for the suggestion. my file is small (only 12 lines). but it still not work. on the other hand, solutions posted here work. so I will go with this. – eagertoLearn Jan 15 '14 at 00:05

8 Answers8

8

Use stdin and you won't have issue with wc printing filename

wc -l < countlines.py
alvits
  • 6,550
  • 1
  • 28
  • 28
  • I think that's a nice solution, but it will only work for a single file per `wc` call. – Uli Köhler Jan 15 '14 at 00:30
  • @UliKöhler - I totally agree with you but this is a direct answer to a question related to single file. It doesn't make sense to only retrieve the sizes of multiple files without knowing which size correspond to which file. If I run `wc -l * | awk '{print $1}'` who is to say which size corresponds to which file? And there will be one extra size output, the total size. – alvits Jan 15 '14 at 00:55
  • I agree. However one valid usecase I can think of (that was not asked for by the OP, but this question might be found by other users) is to list multiple files instead of `*`. Then you know the awk solution will print those lines in the same order as the input files. The total size is indeed a problem for multiple files. – Uli Köhler Jan 15 '14 at 01:08
  • This is not valid with BSD `wc` which also puts whitespace in front of the result if you pipe the contents into it. – Jens Erat Jan 15 '14 at 09:19
3

You can also use awk to count lines. (reference)

awk 'END { print NR }' countlines.py
  • where countlines.py is the file you want to count
csiu
  • 3,159
  • 2
  • 24
  • 26
3

If your file doesn't ends with a \n (new line) the wc -l gives a wrong result. Try it with the next simulated example:

echo "line1"    >  testfile #correct line with a \n at the end
echo -n "line2" >> testfile #added another line - but without the \n

the

$ wc -l < testfile
       1

returns 1. (The wc counts the number of newlines (\n) in a file.)

Therefore, for counting lines (and not the \n characters) in a file, you should to use

grep -c '' testfile

e.g. find empty character in a file (this is true for every line) and count the occurences -c. For the above testfile it returns the correct 2.

Additionally, if you want count the non-empty lines, you can do it with

grep -c '.' file

Don't trust wc :)

Ps: one of the strangest use of wc is

grep 'pattern' file | wc -l

instead of

grep -c 'pattern' file
clt60
  • 62,119
  • 17
  • 107
  • 194
2

is being confused by the leading whitespace.

I'd use to print the 1st field here:

% wc -l countlines.py | awk '{ print $1 }'
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
2

Piping the file name into wc removes it from the output, then translate away the whitespace:

wc -l <countlines.py |tr -d ' '
Jens Erat
  • 37,523
  • 16
  • 80
  • 96
  • All the pipes are unnecessary. By counting from stdin like `wc -l < countlines.py` the utility will only print the number and nothing more. No spaces and no filenames and no need to `tr`. – alvits Jan 15 '14 at 00:02
  • This is implementation-dependent, Gnu `wc` does omit the spaces, BSD `wc` does not. – Jens Erat Jan 15 '14 at 09:18
2

As an alternative, wc won't print the file name if it is being piped input from stdin

$ cat countlines.py | wc -l
9
stellarhopper
  • 660
  • 5
  • 10
  • 1
    Unnecessary to use `cat` and `|` here. A simple `wc -l < countlines.py` will do. Though your answer is better than using `tr` or `awk` or `cut`. – alvits Jan 15 '14 at 00:05
2

yet another way :

cnt=$(wc -l < countlines.py )
echo "total is $cnt "
michael501
  • 1,452
  • 9
  • 18
1

Use awk like this:

wc -l countlines.py | awk {'print $1'}
Uli Köhler
  • 13,012
  • 16
  • 70
  • 120