tags:

views:

89

answers:

4
+4  A: 
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'}
James Broadhead
+1 for a cleaner solution.
casablanca
A: 

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' '`
casablanca
Running wc twice for the same file is inefficient.
gawi
True, that's why I think James Broadhead's answer is much better. I couldn't get `cut` to work on the full output of `wc $f` because the number of spaces between fields varies.
casablanca
You shouldn't have dollar signs on your variables on the LHS and you can't have spaces around the equal sign.
Dennis Williamson
Thanks, don't know what I was thinking. Too much PHP I guess. :)
casablanca
A: 

Try this:

wc `ls` | awk '{ LINE += $1; WC += $2 } END { print "lines: " LINE  " words: " WC }'

It creates a line count, and word count (LINE and WC), and increase them with the values extracted from wc (using $1 for the first column's value and $2 for the second) and finally prints the results.

Aif
+2  A: 

If you redirect the filename into wc it omits the filename on output.

Bash:

read lines words characters <<< $(wc < filename)

or

read lines words characters <<EOF
$(wc < filename)
EOF

Instead of using for to iterate over the output of ls, do this:

for f in *

which will work if there are filenames that include spaces.

If you can't use globbing, you should pipe into a while read loop:

find ... | while read -r f

or use process substitution

while read -r f
do
    something
done < <(find ...)
Dennis Williamson