Problem with while loop in shell script

Hi All,

How to read a file upto last line(End Of Line)
I wrote below program:

cat R2_20060719.610.txt | while read LINE
do
echo "$LINE"
done

above code reading all lines from a file and skipping last line......
is there anything wrong in my code.

Please help me out from this problem. it's little bit urgent.

Regards,
Ravi Kumar Garlapati

cat R2_20060719.610.txt | while read LINE
do
echo "$LINE"
done

or preferably

while read LINE
do
echo "$LINE"
done <R2_20060719.610.txt

Will not skip the last line, it will in effect behave exactly like the cat command. There are a number of ways you could aciehve the desired results. The simplest of which is probably to store the value of LINE in another variable, and print that value in the next iteration through the while loop.

Hi reborg,

thank you for quick reply,

below one is data file:

R2_20060719.610.txt
--------------------
Dividend EventID IssID SecID Created
DIV 170334 3085 3225 2006/06/10
DIV 7034 305 325 2006/06/10 2006/06/12
DIV 3333 30333 325 2006/06/10 2006/06/12

if i use ur code to loop this data

while read LINE
do
echo "$LINE"
done <R2_20060719.610.txt

i am getting output as

Dividend EventID IssID SecID Created
DIV 170334 3085 3225 2006/06/10
DIV 7034 305 325 2006/06/10 2006/06/12

it's skipping last line.

if i use below code

while read LINE
do
echo "$LINE"
done <R2_20060719.610.txt
echo "$LINE"

it's giving me all lines as output including last line.

but my requirement is not that....
within the loop itself i should be able to echo all lines including last line.
there should not be any code outside loop.

Hope this is clear.

Regards,
Ravi Kumar Garlapati

Can you post the output of 'uname -n' and 'echo $0' commands? The code snippet posted by reborg should not be skipping any lines from the file at all...

Hi blowtorch,

Below are the outputs of ur two commands:

uname -n --> ndafedt1
echo $0 --> -ksh

Regards,
Ravi Kumar Garlapati

Oh, sorry, I meant 'uname -a'. Anyway, with ksh you shouldn't be getting such an output. All lines should be printed. Can you just run 'cat -v' on the file and see if any chars are present that shouldn't be there?

Hi blowtorch,

uname -a output is SunOS ndafedt1 5.8 Generic_108528-15 sun4u sparc SUNW,Ultra-Enterprise

when i say cat -v <file name>
it's displaying all lines without any problem(without special or unwanted charecters), displying last line aswell.

again if i try to run below code :

while read LINE
do
echo "$LINE"
done <R2_20060719.610.txt

i am facing the same problem, i.e., skipping the last line.

Regards,
Ravi Kumar Garlapati

Please show us the last lines of your file :

cat -v R2_20060719.610.txt | tail -2

Jean-Pierre.

Hi Jean-Pierre,

My actual data is as below :

Dividend EventID IssID SecID Created
DIV 170334 3085 3225 2006/06/10
DIV 7034 305 325 2006/06/10 2006/06/12
DIV 3333 30333 325 2006/06/10 2006/06/12

cat -v R2_20060719.610.txt | tail -2
when i apply ur above command i am getting below lines as output, in which header is missing :

DIV 170334 3085 3225 2006/06/10
DIV 7034 305 325 2006/06/10 2006/06/12
DIV 3333 30333 325 2006/06/10 2006/06/12

i did not understand what is this ? :confused:

Regards,
Ravi Kumar Garlapati

tail -2 print the two last records of the file.
When you execute the command, there are 3 lines in the output ! :confused:

Execute the following command on your file :

od -cx R2_20060719.610.txt 

Below is the output i got when i execute

od -cx R2_20060719.610.txt

0000000 D i v i d e n d \t E v e n t I D
4469 7669 6465 6e64 0945 7665 6e74 4944
0000020 \t I s s I D \t S e c I D \t C r e
0949 7373 4944 0953 6563 4944 0943 7265
0000040 a t e d \t \n D I V \t 1 7 0 3 3 4
6174 6564 090a 4449 5609 3137 3033 3334
0000060 \t 3 0 8 5 \t 3 2 2 5 \t 2 0 0 6 /
0933 3038 3509 3332 3235 0932 3030 362f
0000100 0 6 / 1 0 \t \n D I V \t 7 0 3 4 \t
3036 2f31 3009 0a44 4956 0937 3033 3409
0000120 3 0 5 \t 3 2 5 \t 2 0 0 6 / 0 6 /
3330 3509 3332 3509 3230 3036 2f30 362f
0000140 1 0 \t 2 0 0 6 / 0 6 / 1 2 \t \n D
3130 0932 3030 362f 3036 2f31 3209 0a44
0000160 I V \t 3 3 3 3 \t 3 0 3 3 3 \t 3 2
4956 0933 3333 3309 3330 3333 3309 3332
0000200 5 \t 2 0 0 6 / 0 6 / 1 0 \t 2 0 0
3509 3230 3036 2f30 362f 3130 0932 3030
0000220 6 / 0 6 / 1 2 \t A \n A \n
362f 3036 2f31 3209 410a 410a
0000234

Regards,
Ravi Kumar Garlapati

Hi All,

Surprisingly my program is working fine now,
it's printing me all the lines of data file.

I did not do any thing to my data file and in script as well.
i thought of testing my luck once, suddenly it started working fine.

i dont understand what was the problem previously. :confused:
i dont understand how it's working now, with out doing anything in either of them(data and program) :confused:

full of confusions, but on the whole it's a nice experience to a fresher to Shell Scripting.

I thank all the people who responded me.
Thaking you all once again. :slight_smile:

Regards,
Ravi Kumar Garlapati

cat -v R2_20060719.610.txt | tail -2
command should have return exactly 2 lines from the file.....can you recreate the file and try......

Hi Dhruva,

now i am getting below 2 lines,

ndafedt1:fed_cntl $ cat -v R2_20060719.610.188 | tail -2
DIV 7034 305 325 2006/06/10 2006/06/12
DIV 3333 30333 325 2006/06/10 2006/06/12

Regards,
Ravi Kumar Garlapati

I guess your script should work now.....or you are facing the same problem...

Hi Dhruva,

If u observe last but one mail of mine, i mentioned how peculiar my unix box is behaving.

anyhow Dhurva, thanks a lot.

Regards,
Ravi Kumar Garlapati

You need to have a carriage return after the last line. vi the file and press 'enter' at the end of last line and save. They retry your code and it should work.

Cheers,
Dinesh

I had the same problem.

All i did was add a blank line at the end of the file

echo >> file.txt

i tried the following but did not succeed

while true
do
{
(echo 1)>1.txt
sleep 1
(echo 0)>1.txt
}
done

try >> instead of >