LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 04-19-2011, 02:34 PM   #1
trynq
LQ Newbie
 
Registered: Feb 2011
Posts: 3

Rep: Reputation: 0
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!
 
Old 04-19-2011, 02:52 PM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,647

Rep: Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524Reputation: 524
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'.
 
Old 04-19-2011, 03:18 PM   #3
lisle2011
Member
 
Registered: Mar 2011
Location: Surrey B.C. Canada (Metro Vancouver)
Distribution: Slackware 2.6.33.4-smp
Posts: 179
Blog Entries: 1

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

please note that wc -l returns the number of lines AND the filename
 
Old 04-19-2011, 03:24 PM   #4
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by lisle2011 View Post
#!/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
 
Old 04-19-2011, 03:33 PM   #5
mande01
LQ Newbie
 
Registered: Aug 2009
Posts: 23

Rep: Reputation: 2
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

Last edited by mande01; 04-19-2011 at 03:34 PM. Reason: Way off the mark!!!
 
Old 04-19-2011, 03:36 PM   #6
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by mande01 View Post
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
 
Old 04-19-2011, 03:38 PM   #7
mande01
LQ Newbie
 
Registered: Aug 2009
Posts: 23

Rep: Reputation: 2
Thanks, your reply totally made me feel better!
 
Old 04-19-2011, 03:42 PM   #8
ozanbaba
Member
 
Registered: May 2003
Location: Tengiz
Distribution: Slackware64 14.1
Posts: 671

Rep: Reputation: 94
Quote:
Originally Posted by AlucardZero View Post
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).
 
Old 04-20-2011, 12:56 AM   #9
trynq
LQ Newbie
 
Registered: Feb 2011
Posts: 3

Original Poster
Rep: Reputation: 0
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
 
Old 04-20-2011, 01:55 AM   #10
ozanbaba
Member
 
Registered: May 2003
Location: Tengiz
Distribution: Slackware64 14.1
Posts: 671

Rep: Reputation: 94
because $var is empty. try this;
Code:
var="$(grep -Fwf file1.txt file2.txt | awk '{if ($6<0.05) print $1,$6}'| wc -l)"
 
1 members found this post helpful.
Old 04-20-2011, 03:39 AM   #11
trynq
LQ Newbie
 
Registered: Feb 2011
Posts: 3

Original Poster
Rep: Reputation: 0
works! thanks a lot. I already got around it but great to know for future. Thanks for all help.
 
Old 04-20-2011, 07:49 AM   #12
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Quote:
Originally Posted by trynq View Post
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.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do you pass an expect command result into a variable and then use it again? edomingox Programming 2 05-09-2010 01:51 AM
How can I receive command output and store it to a variable athlonshi Programming 5 04-04-2010 08:09 AM
how to store a command in a variable lemon09 Programming 11 02-01-2010 02:03 AM
Using variable to store command lines in a pipeline? zizou86 Programming 6 01-29-2010 10:20 AM
How to pass a result of exec command in perl to a variable??? HyperTrey Programming 3 05-23-2008 12:47 PM


All times are GMT -5. The time now is 10:47 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration