xGen
xGen

Reputation: 554

file line count in ksh unix

I am trying to get line count from a file like below in a ksh script. But it returns nothing :

filerecordcount= $((`wc -l <../data/act.dat`))

I also tried these :

    filerecordcount= `wc -l <../data/act.dat`

    filerecordcount= $(wc -l <../data/act.dat)

When i print the variable its not printing the value in the variable.

print "Record Count in .dat file : $filerecordcount." 1>&2;

But when i try the same from command prompt it returns the count

wc -l<../data/act.dat

Upvotes: 1

Views: 3052

Answers (2)

ghoti
ghoti

Reputation: 46856

Simplify simplify. Your backquotes are doing the command expansion, and their output is being processed by $((...)) as an arithmetic expression. It's a little redundant.

filerecordcount=$(wc -l < ../data/act.dat)

No space after the =, and just one level of command expansion.

Alternately you can use process substitution:

read filerecordcount < <(wc -l < ../data/act.dat)

Or you could even do this without a subshell, using a loop:

filerecordcount=0
while read junk; do ((filerecordcount++)); done < ../data/act.dat

Upvotes: 2

user5076313
user5076313

Reputation:

You don't have to give the space after the = in assigning part. Use the below one. It will work fine. But don't forget to print the variable filerecordcount.

UPDATE:

filerecordcount=$((`wc -l <../data/act.dat`))

Upvotes: 3

Related Questions