Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get just the integer from wc in bash

Tags:

bash

wc

People also ask

How do I use wc in bash?

If you only want to print the total number of words in a file along with its name, then you can use the “wc” command with the “-w” flag. Here, you should replace the File with the name of the file whose word count and name you want to be displayed on your terminal.

What does %% mean in bash?

So as far as I can tell, %% doesn't have any special meaning in a bash function name. It would be just like using XX instead. This is despite the definition of a name in the manpage: name A word consisting only of alphanumeric characters and under- scores, and beginning with an alphabetic character or an under- score.


Most simple answer ever:

wc < filename 

Just:

wc -l < file_name

will do the job. But this output includes prefixed whitespace as wc right-aligns the number.


You can use the cut command to get just the first word of wc's output (which is the line or word count):

lines=`wc -l $f | cut -f1 -d' '`
words=`wc -w $f | cut -f1 -d' '`

wc $file | awk {'print "$4" "$2" "$1"'}

Adjust as necessary for your layout.

It's also nicer to use positive logic ("is a file") over negative ("not a directory")

[ -f $file ] && wc $file | awk {'print "$4" "$2" "$1"'}