LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash: reading a particular line (https://www.linuxquestions.org/questions/linux-newbie-8/bash-reading-a-particular-line-230380/)

seb3579 09-14-2004 02:11 AM

bash: reading a particular line
 
Hi,
I am trying to write a script which takes another file and reads in a particular line of the file.

Could somebody please point me in the correct direction as to how i would go about reading the i-th line from the text file please?

Many thanks,
Seb

druuna 09-14-2004 02:17 AM

There is more then one solution to this problem, here's one using sed:

THISLINE=12
sed -n "${THISLINE}p" <filename>


This will print line number 12.

Ranges are also possible:

FIRSTLINE=12
LASTLINE=17
sed -n "${FIRSTLINE},${LASTLINE}p" <filename>


Like I said, this is just one way of doing it.

Hope this helps.

Dark_Helmet 09-14-2004 03:01 AM

Here's another using the head and tail commands:
Code:

line_number=12
target_file="./some_filename"
line_of_text=$( head -n ${line_number} ${target_file} | tail -n 1 )



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