LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 09-14-2004, 02:11 AM   #1
seb3579
LQ Newbie
 
Registered: May 2004
Posts: 13

Rep: Reputation: 0
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
 
Old 09-14-2004, 02:17 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
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.
 
Old 09-14-2004, 03:01 AM   #3
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 368Reputation: 368Reputation: 368Reputation: 368
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 )
 
  


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
bash shell script read file line by line. Darren[UoW] Programming 51 07-04-2013 08:16 PM
BASH: read every line in the files and use the line as parameters as another program tam3c36 Programming 10 12-07-2010 01:42 PM
File reading line by line and compare Goni Linux - Software 14 09-21-2005 12:24 AM
reading from a file line by line Menestrel Programming 5 12-15-2004 02:17 AM
Reading a line Half_Elf Programming 4 09-14-2002 04:49 AM


All times are GMT -5. The time now is 09:31 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration