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

ShellWc

Shell Problem Overview


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?

Shell Solutions


Solution 1 - Shell

Try this way:

wc -l < file.txt

Solution 2 - Shell

cat file.txt | wc -l

According to the man page (for the BSD version, I don't have a GNU version to check):

> If no files are specified, the standard input is used and no file > name is > displayed. The prompt will accept input until receiving EOF, or [^D] in > most environments.

Solution 3 - Shell

To do this without the leading space, why not:

wc -l < file.txt | bc

Solution 4 - Shell

How about

wc -l file.txt | cut -d' ' -f1

i.e. pipe the output of wc into cut (where delimiters are spaces and pick just the first field)

Solution 5 - Shell

Comparison of Techniques

I had a similar issue attempting to get a character count without the leading whitespace provided by wc, which led me to this page. After trying out the answers here, the following are the results from my personal testing on Mac (BSD Bash). Again, this is for character count; for line count you'd do wc -l. echo -n omits the trailing line break.

FOO="bar"
echo -n "$FOO" | wc -c                          # "       3"    (x)
echo -n "$FOO" | wc -c | bc                     # "3"           (√)
echo -n "$FOO" | wc -c | tr -d ' '              # "3"           (√)
echo -n "$FOO" | wc -c | awk '{print $1}'       # "3"           (√)
echo -n "$FOO" | wc -c | cut -d ' ' -f1         # "" for -f < 8 (x)
echo -n "$FOO" | wc -c | cut -d ' ' -f8         # "3"           (√)
echo -n "$FOO" | wc -c | perl -pe 's/^\s+//'    # "3"           (√)
echo -n "$FOO" | wc -c | grep -ch '^'           # "1"           (x)
echo $( printf '%s' "$FOO" | wc -c )            # "3"           (√)

I wouldn't rely on the cut -f* method in general since it requires that you know the exact number of leading spaces that any given output may have. And the grep one works for counting lines, but not characters.

bc is the most concise, and awk and perl seem a bit overkill, but they should all be relatively fast and portable enough.

Also note that some of these can be adapted to trim surrounding whitespace from general strings, as well (along with echo `echo $FOO`, another neat trick).

Solution 6 - Shell

How about

grep -ch "^" file.txt

Solution 7 - Shell

Obviously, there are a lot of solutions to this. Here is another one though:

wc -l somefile | tr -d "[:alpha:][:blank:][:punct:]"

This only outputs the number of lines, but the trailing newline character (\n) is present, if you don't want that either, replace [:blank:] with [:space:].

Solution 8 - Shell

Another way to strip the leading zeros without invoking an external command is to use Arithmetic expansion $((exp))

echo $(($(wc -l < file.txt)))

Solution 9 - Shell

Best way would be first of all find all files in directory then use AWK NR (Number of Records Variable)

below is the command :

find <directory path>  -type f | awk  'END{print NR}'

example : - find /tmp/ -type f | awk 'END{print NR}'

Solution 10 - Shell

This works for me using the normal wc -l and sed to strip any char what is not a number.

wc -l big_file.log | sed -E "s/([a-z\-\_\.]|[[:space:]]*)//g"

# 9249133

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpogibasView Question on Stackoverflow
Solution 1 - ShellNorman RamseyView Answer on Stackoverflow
Solution 2 - ShellpjmorseView Answer on Stackoverflow
Solution 3 - ShellDesi CochraneView Answer on Stackoverflow
Solution 4 - ShellNeil AlbertView Answer on Stackoverflow
Solution 5 - ShellBeejorView Answer on Stackoverflow
Solution 6 - ShellMeIsMichView Answer on Stackoverflow
Solution 7 - ShellBouchaala RedaView Answer on Stackoverflow
Solution 8 - ShellmaxthoursieView Answer on Stackoverflow
Solution 9 - Shelluser128364View Answer on Stackoverflow
Solution 10 - ShelljoseluisqView Answer on Stackoverflow