LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to store result of wc -l command in variable? (http://www.linuxquestions.org/questions/linux-newbie-8/how-to-store-result-of-wc-l-command-in-variable-875954/)

trynq 04-19-2011 02:34 PM

how to store result of wc -l command in variable?
 
Hi guys,

I am new to linux but trying my best. Its my first post but I give up trying to solve it. I want to store the result of wc -l as a variable so I can use it later in my script...so far unsuccessfully.

I have tried this:

set `echo awk '{ print $1, $6}' | wc -l` | echo $1

but it is far from working.

thanks for any help!

AlucardZero 04-19-2011 02:52 PM

This is in a shell script, I take it?

Code:

MYVAR=$(echo awk '{ print $1, $6}' | wc -l)
echo $MYVAR

but I don't think `echo awk '{ print $1, $6}' | wc -l` will return anything other than '1'.

lisle2011 04-19-2011 03:18 PM

#!/bin/bash
MYVAR=$(wc -l ./phpf.php)
echo $MYVAR

please note that wc -l returns the number of lines AND the filename

pwc101 04-19-2011 03:24 PM

Quote:

Originally Posted by lisle2011 (Post 4329784)
#!/bin/bash
MYVAR=$(wc -l ./phpf.php)
echo $MYVAR

please note that wc -l returns the number of lines AND the filename

To eliminate the filename from the wc output, just use standard input instead:
Code:

MYVAR=$(wc -l < ./phpf.php)
echo $MYVAR


mande01 04-19-2011 03:33 PM

I'm completely retracting my suggestion, cause I was way off the mark!!!!






Did you try to redirect it into a file and call it back from there.
I'm only new as well but i think that should work?

http://www.tuxfiles.org/linuxhelp/iodirection.html

e.g wc -l > /home/me/a/file

Derry

pwc101 04-19-2011 03:36 PM

Quote:

Originally Posted by mande01 (Post 4329806)
Did you try to redirect it into a file and call it back from there.
I'm only new as well but i think that should work?

http://www.tuxfiles.org/linuxhelp/iodirection.html

e.g wc -l > /home/me/a/file

Derry

It will work, but it gets messy quickly. You can much more efficiently use a variable in memory (which is very fast) rather than writing to a disk (which is very slow). Whilst this difference in speed is negligible when you're only doing this once or twice, as soon as you start doing this thousands of times, this process really bogs everything down. Writing to a file also means you have to clean up after yourself, which can be a bit of a headache.

Variables were invented to avoid having to write to disk :)

mande01 04-19-2011 03:38 PM

Thanks, your reply totally made me feel better! :)

ozanbaba 04-19-2011 03:42 PM

Quote:

Originally Posted by AlucardZero (Post 4329754)
but I don't think `echo awk '{ print $1, $6}' | wc -l` will return anything other than '1'.

` ` is old notation that people is trying to forget. $( ) notation is posix draft (I forgot which one).

trynq 04-20-2011 12:56 AM

thanks for reply. I still didn't get it to work.

var=$(wc -l) | echo $var

returns nothing. I would like to omit writing the output into the file (need to repeat it 1000 times). The thing is that wc -l command is part of a longer code:

grep -Fwf file1.txt file2.txt | awk '{if ($6<0.05) print $1,$6}'| var=$(wc -l) | echo $var

ozanbaba 04-20-2011 01:55 AM

because $var is empty. try this;
Code:

var="$(grep -Fwf file1.txt file2.txt | awk '{if ($6<0.05) print $1,$6}'| wc -l)"

trynq 04-20-2011 03:39 AM

works! thanks a lot. I already got around it but great to know for future. Thanks for all help.

MTK358 04-20-2011 07:49 AM

Quote:

Originally Posted by trynq (Post 4330160)
var=$(wc -l) | echo $var r

Setting a variable does not emit any output, and "echo" doesn't need any input to get $var's value. Also, you aren't piping anything into wc. And lastly, commands separated by pipes are executed at the same time, not in sequence.


All times are GMT -5. The time now is 02:05 PM.