LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 06-19-2008, 05:19 PM   #1
pdklinux79
LQ Newbie
 
Registered: Jun 2008
Posts: 29

Rep: Reputation: 15
reading a file


how to get total number of lines in a file
how to read a particular line, eg line 7 in that file.
 
Old 06-19-2008, 05:28 PM   #2
amani
Senior Member
 
Registered: Jul 2006
Location: Kolkata, India
Distribution: Debian 64-bit GNU/Linux, Kubuntu64, Fedora QA, Slackware,
Posts: 2,766

Rep: Reputation: Disabled
less will provide line numbers. You just need to grep what you want.
Or you can use cat and grep.
Or vim


Best

A. Mani
 
Old 06-19-2008, 05:42 PM   #3
bsdunix
Senior Member
 
Registered: May 2006
Distribution: BeOS, BSD, Caldera, CTOS, Debian, LFS, Mac, Mandrake, Red Hat, Slackware, Solaris, SuSE
Posts: 1,761

Rep: Reputation: 80
You can use wc for number lines:
WC
http://www.linuxmanpages.com/man1/wc.1.php

and sed to show line number:
HANDY ONE-LINERS FOR SED
http://www.linuxhowtos.org/System/se...m?ref=news.rdf

and there's probably other ways to do the same thing, that's the beauty of *nix.
 
Old 06-19-2008, 06:05 PM   #4
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by bsdunix View Post
and there's probably other ways to do the same thing, that's the beauty of *nix.
Here’s a few ways to show a particular line of a file (in these examples, show line 42):
Code:
awk 'NR==42' filename
Code:
sed -n '42p' filename
Code:
head -42 filename | tail -1
 
Old 06-19-2008, 06:06 PM   #5
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Btw, the GNU version of cat will number lines for you:
Code:
cat -n filename
 
Old 06-20-2008, 12:36 AM   #6
LlNUX
Member
 
Registered: Oct 2007
Location: Sydney, Australia
Distribution: Debian
Posts: 63

Rep: Reputation: 15
>how to get total number of lines in a file
Check Linux commands for wc command and/or nl command or above mention cat command.
or use vim editor

>how to read a particular line, eg line 7 in that file.

this depends on how you want to approach this problem. You can use vim editor again or you can use tail and head commands which is not really nice solution or you can write Perl script to do it.

Hope this helps
 
  


Reply



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
how to create a new file including content from reading another file !? silverhand Programming 2 10-06-2007 04:33 PM
Reading/Wirting file/parsing xml file using javascript fakhrul Programming 1 08-14-2007 05:08 PM
Reading text file-writting binary file cdog Programming 5 06-13-2006 11:56 AM
awk: fatal:cannot open file for reading (no such file or Directory) in Linux sangati vishwanath Linux - Software 4 07-06-2005 12:59 AM
Script, Reading a file, When end of file? elibm Programming 2 07-16-2001 11:01 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:56 AM.

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
Open Source Consulting | Domain Registration