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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
04-19-2011, 02:34 PM
|
#1
|
LQ Newbie
Registered: Feb 2011
Posts: 3
Rep:
|
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!
|
|
|
04-19-2011, 02:52 PM
|
#2
|
Senior Member
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824
|
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'.
|
|
|
04-19-2011, 03:18 PM
|
#3
|
Member
Registered: Mar 2011
Location: Surrey B.C. Canada (Metro Vancouver)
Distribution: Slackware 2.6.33.4-smp
Posts: 183
Rep:
|
#!/bin/bash
MYVAR=$(wc -l ./phpf.php)
echo $MYVAR
please note that wc -l returns the number of lines AND the filename
|
|
|
04-19-2011, 03:24 PM
|
#4
|
Senior Member
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847
Rep:
|
Quote:
Originally Posted by lisle2011
#!/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
|
|
|
04-19-2011, 03:33 PM
|
#5
|
LQ Newbie
Registered: Aug 2009
Posts: 23
Rep:
|
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!!!
|
|
|
04-19-2011, 03:36 PM
|
#6
|
Senior Member
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847
Rep:
|
Quote:
Originally Posted by mande01
|
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
|
|
|
04-19-2011, 03:38 PM
|
#7
|
LQ Newbie
Registered: Aug 2009
Posts: 23
Rep:
|
Thanks, your reply totally made me feel better!
|
|
|
04-19-2011, 03:42 PM
|
#8
|
Member
Registered: May 2003
Location: İzmir
Distribution: Slackware64 15.0 Multilib
Posts: 778
Rep:
|
Quote:
Originally Posted by AlucardZero
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).
|
|
|
04-20-2011, 12:56 AM
|
#9
|
LQ Newbie
Registered: Feb 2011
Posts: 3
Original Poster
Rep:
|
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
|
|
|
04-20-2011, 01:55 AM
|
#10
|
Member
Registered: May 2003
Location: İzmir
Distribution: Slackware64 15.0 Multilib
Posts: 778
Rep:
|
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.
|
04-20-2011, 03:39 AM
|
#11
|
LQ Newbie
Registered: Feb 2011
Posts: 3
Original Poster
Rep:
|
works! thanks a lot. I already got around it but great to know for future. Thanks for all help.
|
|
|
04-20-2011, 07:49 AM
|
#12
|
LQ 5k Club
Registered: Sep 2009
Posts: 6,443
|
Quote:
Originally Posted by trynq
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.
|
All times are GMT -5. The time now is 12:15 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|