LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Display/Read line 'N' in a text file using script (https://www.linuxquestions.org/questions/linux-newbie-8/display-read-line-n-in-a-text-file-using-script-103332/)

ganninu 10-13-2003 03:02 AM

Display/Read line 'N' in a text file using script
 
I was just wondering if there exists already an inbuilt Linux command to just display a particular line or range of lines in a text file. I normally use a "for" loop for this kind of thing and works well.

iainr 10-13-2003 05:20 AM

There may be a more elegant way, but if, for example, I wanted to display lines 23-30 I would use

head -30 <file> | tail -8

Or more generally, in a script

FIRST_LINE=$1
LAST_LINE=$2
typeset -i NUM_LINES=$LAST_LINE-$FIRST_LINE+1
head -$LAST_LINE|tail -$NUM_LINES

(I only just tried this code in ksh so apologies if it needs to be tweaked for bash).

Iain.

ganninu 10-13-2003 05:28 AM

very elegant technique indeed...


All times are GMT -5. The time now is 10:40 PM.