I'm running this series of commands
passwd=`wc -l /etc/passwd`
echo $passwd
Returns:
34 /etc/passwd
What do I need to do to this so that it will only show the output of wc -l
?
Just read from standard input instead of giving wc
a file name:
$ passwd=`wc -l < /etc/passwd`
$ echo "$passwd"
86
wc
still outputs quite a bit of padding, but the file name is omitted (because wc
has no idea what file the data comes from).
That's the default behaviour of wc
:
» wc -l /etc/passwd
28 /etc/passwd
There is no way to tell wc not to output the filename.
Using awk
perhaps ?
$ passwd=$(wc -l /etc/passwd | awk '{print $1}')
$ echo $passwd
32
Using cut
, from cut (GNU coreutils)
$ passwd=$(wc -l /etc/passwd | cut -d" " -f1)
$ echo $passwd
32
wc
returns also the filename, but there are other ways to do it. Some examples:
passwd=`wc -l /etc/passwd | grep -o [1-9]\*`
or
passwd=`wc -l /etc/passwd | cut -f1 -d' '`
(answer from this question: get just the integer from wc in bash)