Help Required regarding wc command

Hi guys,

I want to find the number of records in a particular file and store that value in any other variable. I am trying this below command but it is not working and giving me an error "Uninary Operator Expected".

say I have taken a variable name 'count' in which I have to store the no. of records in a file.

count= wc -l <file name>

can u guys pls help me out...how i can store a no. of records in a variable

set `wc -l file_name`
count=$1
echo $count

count=`wc -l test1`
use wc in back quotes. it works.ok

thanks guys its solve my problem :slight_smile:

Because sometimes they can confuse...

>count=$(wc -l file_name)

I like this because whatever I would normally type at the command prompt I simply put inside the parentheses.

I also prefer this approach. For reference, surrounding the statement in parenthesis actually creates a sub-shell (another process). Putting the $ in front of it lets you use the result as a value.

Hi,

I want to store the count for a number of occurrences of a string \(Ex: Sample.test.test1\) in a variable from a file.

For the above string format, please tell me how to find it.

Also, I have one more question.


I want to count the number of occurrences of a sentence in file and this count needs to be stored in a variable.

Anyone, please help in solving the above two issues ASAP.

Regards,
Prasanna

count=`grep 'sample.test.test1' <filename> | wc -l