Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Number of Lines in file

Status
Not open for further replies.

tokerago13

Programmer
Jan 10, 2002
35
GB
Hi,
I'm trying to count the total number of lines in a file.
When I use wc -l FILENAME, I get the number of lines and the filename. I only want the number returned to use in my script.
How do I go about this?

T
 
not elegant, but:

wc -l filename | tr -s ' ' ' ' | cut -f2 -d' '

does it. That reminds me - must learn Awk for Christmas!
 
cat filename | wc -l

I love deadlines. I like the whooshing sound they make as they fly by - Douglas Adams
 
Hi all,
Thanks fro replying I got the value I wanted. What I would like to know is... Is the value returned a numerical value. I'm currently doing this

PAYCOUNT=`wc -l < ${OUT_DIR}/${FILE_NAME}`
if [ $PAYCOUNT -gt 1 ] then
sendmail......

And I get the error

+ [ -gt 1 ]
test: argument expected

What am I missing?

Tx

T
 
It is a number that is returned. The only way the error would be displayed is if the file didn't exist. But then you would get another (file not found error).

Try echoing the value of PAYCOUNT just after it is set.

more k.ksh
PAYCOUNT=`wc -l < /etc/hosts`
echo $PAYCOUNT
if [ $PAYCOUNT -gt 1 ]
then
echo sendmail......
fi

root@i86sol: /var/tmp# ./k.ksh
5
sendmail......


and if the file doesn't exist...

more k.ksh
PAYCOUNT=`wc -l < /etc/k.k`
echo $PAYCOUNT
if [ $PAYCOUNT -gt 1 ]
then
echo sendmail......
fi

root@i86sol: /var/tmp# ./k.ksh
./k.ksh: /etc/k.k: cannot open

./k.ksh[3]: test: argument expected

Hope this helps...

I love deadlines. I like the whooshing sound they make as they fly by - Douglas Adams
 
tokerago13:

Provided $FILE_NAME exists, your original syntax works on my Solaris 7 system using either Bourne or Korn shells:

PAYCOUNT=`wc -l < ${OUT_DIR}/${FILE_NAME}`
if [ $PAYCOUNT -gt 1 ]
then

Could it be you are using a very old copy of sh on SCO? If so, you can try using > instead of -gt:


if [ $PAYCOUNT > 1 ]
then
echo &quot;greater than 1&quot;
fi

try using the expr command:

if expr $PAYCOUNT \> 1 > /dev/null
then
echo &quot;greater than 1&quot;
fi

Regards,

Ed
 
b]test: argument expected[/b] error msg implies that your variable $PAYCOUNT is null
change
Code:
if [ $PAYCOUNT -gt 1 ]
to
Code:
if [ &quot;$PAYCOUNT&quot; -gt 1 ]
the diff is $PAYCOUNT in quotes and space after left bracket [

however this would only eliminate the
test: argument expected error msg

But u still have to ivestigate what are u getting the value of $PAYCOUNT. or possibly u are making spelling mistake



[ponder]
----------------
ur feedback is a very welcome desire
 
Thanks all. It works when I don't use the $ sign for the variable.
Another slight problem.. whn I do the following

PAYCOUNT=`wc -l < ${FINAL_NAME}`

PAYCOUNT= 12

if [ PAYCOUNT -gt 1 ] then
sendmail.....
but what happens is that PAYCOUNT does not recognise the number returned 12 because of the spaces in front of it.
I don't know sed which will be able to remove the spaces.

What I would like to get is
PAYCOUNT=12

what sed command do I need?

T
 
In the absence of sed:

PAYCOUNT=`wc -l < ${FINAL_NAME} | tr -s ' ' '' | cut -f2 -d' '`
 
Thanks for all the input.
I solved my problem by doing the following

PAYCOUNT=`wc -l < ${FINAL_NAME}`
PAYCOUNT=`echo $FSA_PAYCOUNT`;

echo &quot;Success file found&quot;
if [ $PAYCOUNT -gt 1 ] then
senmail....
This removed the whitespaces

Thanks once again
 
In ksh you can do something like this:
Code:
typeset -i PAYCOUNT=`wc -l < ${FINAL_NAME}`

Hope This Help
PH.
 
when u use gt or eq or similar u convert the comaparison into a numeric comaprison.
then
Code:
PAYCOUNT=    12
is same as
Code:
 PAYCOUNT=12
implication -- try following code
Code:
a=&quot;  abc&quot;  # two spaces
[ $a = abc] ] or [ $a = &quot;abc&quot; ]
results true (check by echo $?)
but [ &quot;$a&quot; = &quot;abc&quot; ]
results false. here [ &quot;$a&quot; = &quot;  abc&quot; ]  #abc with two spaces
only results true
However when we convert this into numeric then
a='   403'
and [ $a -gt 402 ] or [ &quot;$a&quot; -gt 402 ] both are true
u can try this in sh or ksh with v and x options on i.e
set -vx on. this gives a better understanding o spaces.

However what is $FSA is not clear

PAYCOUNT=`wc -l < ${FINAL_NAME}`
PAYCOUNT=`echo $FSA_PAYCOUNT`;




[ponder]
----------------
ur feedback is a very welcome desire
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top