Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unix - getting a string line in a file

Status
Not open for further replies.

kate007

Programmer
May 15, 2002
7
NZ
Hi,
I have flat file with multiple lines as below
++++++++++++ file_name : test.txt
This is a first row
This is a second row
This is a third row and soon ...
++++++++++++
Is there a way that i can get each line into a variable. I tried with foreach command but i am n't getting it and i'm using C-shell

foreach row in (`cat test.txt`)
set string=$row
echo $string
end

Output was ::
This
is
a
first
row

But i need as a single line "This is a first row ""
Any suggestions and how to get this done >>
Thanks a lot
Kate
 
Hi Kate,

Try this

while Line=`line`
do
echo " $Line"
done < filename

This should work in bourne/K shell.

Mohan
 
one thing you can do is this:

while read line
do
string=$line
done < test.txt
 
None of the above two while loops doesn't worked either.
Any other commands in unix to get the line as as whole into a variable

Thanks a lot
Kate
 
I think you need to use the 'read' command as in :-

read line < filename
echo $line

Then the tricky bit is to build this into a loop
 
Try this ....

count=1
while (($count < 10))
do
tail +$count filename | head -1 > tempfile
read line < tempfile
echo $line
let &quot;count=count + 1&quot;
done

This example assumes there are 10 lines in the file.
The tail / head line extracts each line in turn into a temp file.

Hope this helps.

 
What about ...

cat filename |
while read line
do
echo $line
done

Greg.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top