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

BashWc

Bash Problem Overview


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 script is run:

121711 /home/slash/.java_base.tag lines
121711 /home/slash/.java_base.tag lines
121711 /home/slash/.java_base.tag lines

I.e. the name of the file is also echoed (which I don't want to). Why do these scriplets fail and how should I output a clean:

121711 lines

?

Bash Solutions


Solution 1 - Bash

An Example Using Your Own Data

You can avoid having your filename embedded in the NUMOFLINES variable by using redirection from JAVA_TAGS_FILE, rather than passing the filename as an argument to wc. For example:

NUMOFLINES=$(wc -l < "$JAVA_TAGS_FILE")
Explanation: Use Pipes or Redirection to Avoid Filenames in Output

The wc utility will not print the name of the file in its output if input is taken from a pipe or redirection operator. Consider these various examples:

# wc shows filename when the file is an argument
$ wc -l /etc/passwd
41 /etc/passwd

# filename is ignored when piped in on standard input
$ cat /etc/passwd | wc -l
41

# unusual redirection, but wc still ignores the filename
$ < /etc/passwd wc -l
41

# typical redirection, taking standard input from a file
$ wc -l < /etc/passwd
41

As you can see, the only time wc will print the filename is when its passed as an argument, rather than as data on standard input. In some cases, you may want the filename to be printed, so it's useful to understand when it will be displayed.

Solution 2 - Bash

wc can't get the filename if you don't give it one.

wc -l < "$JAVA_TAGS_FILE"

Solution 3 - Bash

You can also use awk:

awk 'END {print NR,"lines"}' filename

Or

awk 'END {print NR}' filename

Solution 4 - Bash

(apply on Mac, and probably other Unixes)

Actually there is a problem with the wc approach: it does not count the last line if it does not terminate with the end of line symbol.

Use this instead

nbLines=$(cat -n file.txt | tail -n 1 | cut -f1 | xargs)

or even better (thanks gniourf_gniourf):

nblines=$(grep -c '' file.txt)

Note: The awk approach by chilicuil also works.

Solution 5 - Bash

It's a very simple:

NUMOFLINES=$(cat $JAVA_TAGS_FILE | wc -l )

or

NUMOFLINES=$(wc -l $JAVA_TAGS_FILE | awk '{print $1}')

Solution 6 - Bash

I normally use the 'back tick' feature of bash

export NUM_LINES=`wc -l filename`

Note the 'tick' is the 'back tick' e.g. ` not the normal single quote

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
QuestionMarcus Junius BrutusView Question on Stackoverflow
Solution 1 - BashTodd A. JacobsView Answer on Stackoverflow
Solution 2 - BashIgnacio Vazquez-AbramsView Answer on Stackoverflow
Solution 3 - BashJavier LópezView Answer on Stackoverflow
Solution 4 - BashlingView Answer on Stackoverflow
Solution 5 - BashSlava SemushinView Answer on Stackoverflow
Solution 6 - BashRuss HoreView Answer on Stackoverflow