LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to count the number of llines inside a file and put the output into a variable? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-count-the-number-of-llines-inside-a-file-and-put-the-output-into-a-variable-859937/)

auma78 02-01-2011 03:27 AM

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 :)

grail 02-01-2011 03:38 AM

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 :)

auma78 02-01-2011 05:59 AM

hi

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

thanks for the help :)

Nylex 02-01-2011 06:21 AM

Quote:

Originally Posted by auma78 (Post 4244484)
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.


All times are GMT -5. The time now is 09:11 PM.