Discussion Locked

line count

0 helpful
7 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

e.g. wc -l filename
gives: 2000 filename

This gives the # of lines and also shows the filename.

I want to extract the # of lines only (2000) and not show the filename. I want to try to do this in one line and not have temporary lines.

Thanks

7 replies

by cactus pete

You could pipe it to awk and remove just the one space delimited column you wanted...

wc -l filename |awk $1

That might do it, though I don't have a nix box at my fingertips right now

by cactus pete

Actually, that would be wrong [syntax is different], and there's something perhaps better:

cat filename | wc -l

That should do it.

by gary_w

Take it a step further and get rid of leading blanks or tabs too by assigning the result to an integer variable. Using the korn shell, which supports this:

#!/usr/bin/ksh
integer linecount=`cat filename|wc -l`

Use $linecount to refer to the linecount in your script as:

echo $linecount

Gary

by rgooding

I already have a file with one line. The contents of the line looks like: 4000 filename.txt.

What I need is to get the "4000" from the line.

Hope this explains it further...Thanks agin

by gary_w

Ok, here ya go:

# Cut field 1 where the delimiter is a space.
integer linecount=`cat filename|cut -f1 -d' '`

echo $linecount

Mote that there is a space between the single quotes.

Gary

This discussion is closed to new replies.