LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 02-01-2008, 01:39 AM   #1
sharath.bv
LQ Newbie
 
Registered: Jan 2008
Posts: 17

Rep: Reputation: 0
"How to read lines from a file"


Hi,

I want to read lines from a file line-by-line and to display those lines on the terminal.
Can any one please provide a shell script for this.
 
Old 02-01-2008, 01:49 AM   #2
lazlow
Senior Member
 
Registered: Jan 2006
Posts: 4,363

Rep: Reputation: 172Reputation: 172
cat filename | more -1
The "|" is the pipe sysmbol (shift \ on my keyboard).
 
Old 02-01-2008, 01:55 AM   #3
eoo
LQ Newbie
 
Registered: Nov 2003
Distribution: SuSE 8.1
Posts: 2

Rep: Reputation: 0
Hi,

I'm not sure what you're trying to do, but I'd use sed, e.g.

Quote:
sed -n "${i}p" $file
A longer longer example would be something like

Quote:
#! /bin/bash

file=/etc/fstab
nlines=$(wc -l $file | cut -f1 -d' ')
i=0

while [ $i -le $nlines ]
do
echo "Give me a line number"
read i
sed -n "${i}p" $file
done

echo "Reached end"
Hope this helps

Cheers
 
Old 02-01-2008, 02:34 AM   #4
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
Quote:
Originally Posted by sharath.bv View Post
Hi,

I want to read lines from a file line-by-line and to display those lines on the terminal.
Can any one please provide a shell script for this.
This is obviously just a starting point, so please forgive me for asking.

What are you trying to achieve?

To read each line into a variable you could do:
Code:
#!/bin/bash
cat myfile|while read line
do
   ##perform some checks
   echo $line
done
However, this strips out any leading spaces or tab characters, therefore you may not get what you are after.

To do the same thing in Perl (which doesn't strip out leading spaces or tab characters):
Code:
#!/usr/bin/perl
open(INPUT, "myfile") or die "Could not open input file";

while (<INPUT>) {
   ##perform some checks
   print;
}

close(INPUT);
 
Old 02-01-2008, 03:41 AM   #5
sharath.bv
LQ Newbie
 
Registered: Jan 2008
Posts: 17

Original Poster
Rep: Reputation: 0
Thanks

Thanks all,
i got the answer.........
 
Old 02-01-2008, 07:47 AM   #6
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
The simplest and most readable way is (IMO):
Code:
while read line
do
  echo $line
done < file

Last edited by H_TeXMeX_H; 02-01-2008 at 07:52 AM.
 
  


Reply



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
gdb error "Failed to read a valid object file image from memory." Fejimush Programming 0 03-11-2007 01:40 PM
6.06 Dapper: Need NFS client but cannot read the KDE "File Sharing" screen lefty.crupps Ubuntu 2 02-15-2007 01:36 PM
"Read-Only File System" external USB HD logan2004 Linux - Hardware 3 04-01-2005 09:07 AM
Can't boot Mandrake 10.0, "Cannot touch, Read-Only File System" RasutoIbuki Mandriva 5 07-23-2004 08:57 PM
"mkdir: cannot create directory `foo': Read-only file system" on FAT32 maddes Linux - Hardware 1 11-26-2003 06:19 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:23 PM.

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
Open Source Consulting | Domain Registration