LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 02-01-2011, 03:27 AM   #1
auma78
LQ Newbie
 
Registered: Jan 2011
Posts: 10

Rep: Reputation: 0
Smile how to count the number of llines inside a file and put the output into a variable?


hi
i need to count the number of files and put the output into a variable.
i used
wc -l filename
but i couldnt find an option to put the output to variable. example if the number o line is 5, i need the output of echo $x is 5.
can anyone help please
 
Old 02-01-2011, 03:38 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,561

Rep: Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939
Well there are other issues with your command in that it includes other data than the number of lines. I will let you work out how to get rid of that, but
have a look at your favourite bash site about backticks (``) or $() features.

Clue: These are synonymous but one has the advantage of readability and negates the need for escaping in some circumstances
 
1 members found this post helpful.
Old 02-01-2011, 05:59 AM   #3
auma78
LQ Newbie
 
Registered: Jan 2011
Posts: 10

Original Poster
Rep: Reputation: 0
hi

it should be:
x=$(cat filename | wc -l)

thanks for the help
 
Old 02-01-2011, 06:21 AM   #4
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Quote:
Originally Posted by auma78 View Post
x=$(cat filename | wc -l)
You could also eliminate the 'cat' and use awk to get just the number of lines:

wc -l filename | awk '{print $1}'

It's just another way to do it.
 
  


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
[SOLVED] Need help!!How to extract text in one file as variable then count it in another file? nobtiba Programming 14 01-04-2011 01:37 AM
how to delete last number/word of a file and incude file count at the end of the chennaiguy Linux - Newbie 2 02-18-2008 09:08 PM
Script: put the output of a command into a variable. poincare999 Programming 4 11-04-2007 11:53 AM
unable to put file 4GB file INSIDE iso saravkrish Linux - Software 2 03-23-2005 03:41 PM
Count number of line in a file. philipina Programming 7 03-18-2004 05:04 PM


All times are GMT -5. The time now is 06:32 PM.

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