for i acting funny

I have a file named test. In that file I have:
Code:
a,1
b,2
c,3
d,4


Now in my program I have this:

Code:
clear
mc=0
IFS=,

for i in `cat test` ; do
mc=$(( $mc + 1 ))
echo $mc $i

The output is:
Code:
1 a
2 1
b
3 2
c
4 3
d
5 4

Without IFS=, it looks like this:
Code:
1 a,1
2 b,2
3 c,3
4 d,4

The output I'm looking for is:
Code:
1 a
2 1
3 b
4 2
5 c
6 3
7 d
8 4


Could someone please explain why it is that b comes out on the 3rd line?

The purpose is to have $i set to var1, and then the next iteration to var2, have those run commands, then put back into a file. But since I can't explain how b ends up displaying without cause, I can't continue.

Thanks!
 
Code:
#!/bin/sh

clear
mc=0
IFS=',
'

for i in `cat test` ; do
mc=$(( $mc + 1 ))
echo $mc $i
done

IFS should include newline character.
 
I prefer this:

Code:
#!/bin/sh

clear
mc=0
IFS=,

while read CHAR NUM; do
  mc=$(( $mc + 1 ))
  echo $mc $CHAR
  mc=$(( $mc + 1 ))
  echo $mc $NUM
done < test
 
Back
Top