LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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, 06: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, 06: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, 06:42 PM   #3
bsdunix
Senior Member
 
Registered: May 2006
Distribution: Caldera, CTOS, Debian, FreeBSD, Mac OS X, Mandrake, Minix, OpenBSD, Slackware, SuSE
Posts: 1,757

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, 07:05 PM   #4
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 75
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, 07:06 PM   #5
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 75
Btw, the GNU version of cat will number lines for you:
Code:
cat -n filename
 
Old 06-20-2008, 01: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


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


All times are GMT -5. The time now is 10:58 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration