LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 10-24-2007, 03:47 AM   #1
loplayers
LQ Newbie
 
Registered: Oct 2007
Posts: 14

Rep: Reputation: 0
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
 
Old 10-24-2007, 03:57 AM   #2
Dinithion
Member
 
Registered: Oct 2007
Location: Norway
Distribution: Slackware 14.1
Posts: 446

Rep: Reputation: 59
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}'`
 
Old 10-24-2007, 04:10 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Another way, using only awk can be
Code:
variable=$(gawk 'END{print NR}' filename)
here I used the $( ) form for command substitution.
 
  


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
highlight text of a file from specified line number tanveer Linux - General 9 12-06-2006 10:27 PM
Getting Line Number in Error Log File kushalkoolwal Programming 6 10-17-2005 12:50 PM
Display line number n from a file doctorwebbox Linux - Newbie 2 01-05-2005 02:06 PM
Print line number X of a file (in shell) rheza Programming 4 01-04-2005 05:55 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 05:20 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration