LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to get the number of line of a file? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-get-the-number-of-line-of-a-file-594137/)

loplayers 10-24-2007 03:47 AM

how to get the number of line of a file?
 
i want to use a variable to save the number of line of a file~
what is the syntax and command. Thanks

Dinithion 10-24-2007 03:57 AM

you could use 'wc -l <file>' to get the number of lines in a file. The same command (wc) can be used to count various things. To store in a variable, you could use this command:
export variable_name=`wc -l filename.txt | awk '{print $1}'`

colucix 10-24-2007 04:10 AM

Another way, using only awk can be
Code:

variable=$(gawk 'END{print NR}' filename)
here I used the $( ) form for command substitution.


All times are GMT -5. The time now is 07:02 AM.