The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Reading the particular line from the file eamani_sun Shell Programming and Scripting 3 05-16-2008 01:55 PM
reading text file line by line MizzGail Shell Programming and Scripting 6 04-14-2008 03:58 AM
Line by line file reading... and more! ProFiction Shell Programming and Scripting 6 07-26-2007 08:32 AM
Reading line by line from a file tej.buch Shell Programming and Scripting 2 01-22-2006 10:50 PM
Reading line by line from file. akpopa UNIX for Dummies Questions & Answers 4 08-30-2001 07:20 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-12-2007
Registered User
 

Join Date: Jul 2006
Posts: 145
reading each line from a file

hi gurus.. i have a requirement like this...I need to read each line in a file and store it in a variable.. the file is li,e this. i would prefer to construct a loop for this.

1
2
3
4
5
6
7

i need something like this, while (i< last record in file)
x= the first record
do some stuff with x
(i=i+1)

read the next record in the next iteration.

thanks in advance
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-12-2007
Shell_Life's Avatar
Unix/Informix/4GL/SQL
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
ragha81,
Here is one way of looping thru a file:
Code:
while read EachLine
do
   echo $EachLine
done < input_file
Reply With Quote
  #3 (permalink)  
Old 04-12-2007
Registered User
 

Join Date: Jul 2006
Posts: 145
thanks a lot for your timely help buddy.. ur code worked the way i wanted!
Reply With Quote
  #4 (permalink)  
Old 04-13-2007
Registered User
 

Join Date: Feb 2006
Location: msk.ru.earth
Posts: 43
another way for bash/csh
Code:
for i in `cat input_file` ; do
echo $i 
done
Reply With Quote
  #5 (permalink)  
Old 04-13-2007
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,643
See 12 Ways to Parse a file
Reply With Quote
  #6 (permalink)  
Old 04-13-2007
cfajohnson's Avatar
Registered User
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 487
Quote:
Originally Posted by Shell_Life
ragha81,
Here is one way of looping thru a file:
Code:
while read EachLine
do
   echo $EachLine
done < input_file

That script will strip leading and trailing spaces from the lines, and will treat backslashes as escape characters.

To prevent stripping spaces, set IFS to an empty string.

To prevent backslash interpretation, use the -r option:

Code:
while IFS= read -r EachLine
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 10:38 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0