LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   reading a file (https://www.linuxquestions.org/questions/linux-newbie-8/reading-a-file-650404/)

pdklinux79 06-19-2008 05:19 PM

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.

amani 06-19-2008 05:28 PM

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

bsdunix 06-19-2008 05:42 PM

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

osor 06-19-2008 06:05 PM

Quote:

Originally Posted by bsdunix (Post 3189771)
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

osor 06-19-2008 06:06 PM

Btw, the GNU version of cat will number lines for you:
Code:

cat -n filename

LlNUX 06-20-2008 12:36 AM

>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


All times are GMT -5. The time now is 03:30 AM.