Operating System - HP-UX
1828415 Members
3432 Online
109977 Solutions
New Discussion

read complete lines in a for loop

 
SOLVED
Go to solution
Oliver Charni
Trusted Contributor

read complete lines in a for loop

hi all!

Can anyone help me out here, I need to read completes Lines of a File in a for loop.
for i in //filename//
and i is supposed to be the first, second etc. line of that file.

Anyone got an Idea how I could do that ?

kind regards
Oliver
if it smell's funny on the outside, it's worse on the inside
9 REPLIES 9
Bill McNAMARA_1
Honored Contributor

Re: read complete lines in a for loop

for i in $(cat filename)
do
echo $i
done

Later,
Bill
It works for me (tm)
Darrell Allen
Honored Contributor
Solution

Re: read complete lines in a for loop

Hi Oliver,

I prefer a while loop:

while read line
do
echo "$line"
done
Quoting $line maintains spacing.

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
Justo Exposito
Esteemed Contributor

Re: read complete lines in a for loop

Hi Oliver,

You must beware with the spaces or tabs character into the file, this is because the for structure takes this as a separator.

If you have spaces in your file you must replace (use sed) by a different character in order to use the whole line as one parameter.

Hope this helps,

Justo.
Help is a Beatiful word
S.K. Chan
Honored Contributor

Re: read complete lines in a for loop

With the "while" loop you have more control. For example reading /etc/passwd using ":" as field separator.

IFS=:
exec 0while read -r Name Pass Uid Gid Comm Home Shell
do
print "$Name $Uid $Gid"
done
Shahul
Esteemed Contributor

Re: read complete lines in a for loop

Hi

Please try this..I have tested now

for i in "`cat file`"
do
echo "$i"
done


Best of luck
Shahul
Steven Sim Kok Leong
Honored Contributor

Re: read complete lines in a for loop

Hi,

Here's another method (takes care of spaces and tabs):

#!/usr/bin/sh

inc=1
total=`cat $1 | wc -l`

while [ "$inc" -le "$total" ]
do
tail -$inc $1 | head -1
inc=`expr $inc + 1`
done

Hope this helps. Regards.

Steven Sim Kok Leong
Steven Sim Kok Leong
Honored Contributor

Re: read complete lines in a for loop

Hi,

Sorry, made a typo (swapped the head and tail). Should be:

#!/usr/bin/sh

inc=1
total=`cat $1 | wc -l`

while [ "$inc" -le "$total" ]
do
head -$inc $1 | tail -1
inc=`expr $inc + 1`
done

Hope this helps. Regards.

Steven Sim Kok Leong
A. Clay Stephenson
Acclaimed Contributor

Re: read complete lines in a for loop

Here is yet another (and the really classic) method - reading using a file descriptor:

INFILE="myfilename"

exec 9<${INFILE}
X=$(line <&9)
STAT=$?
while [ ${STAT} -eq 0 ]
do
echo "${X}"
X=$(line <&9)
STAT=$?
done

Reading using a file descriptor reads the lines sequentially.

If it ain't broke, I can fix that.
H.Merijn Brand (procura
Honored Contributor

Re: read complete lines in a for loop

Read file by line and print;

# perl -pe 1 file

Read file by line and insert line number at front

# perl -pe 's/^/$. /' file

Read file by line and make it useful (if the content is possitive)

# perl -pe 's/ms-?dos/hp-ux/ig' file
Enjoy, Have FUN! H.Merijn