# for i  acting funny



## GhettoBSD (Nov 10, 2009)

I have a file named test. In that file I have:

```
a,1
b,2
c,3
d,4
```


Now in my program I have this:


```
clear
mc=0
IFS=,

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

The output is:

```
1 a
2 1
b
3 2
c
4 3
d
5 4
```

Without IFS=, it looks like this:

```
1 a,1
2 b,2
3 c,3
4 d,4
```

The output I'm looking for is:

```
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!


----------



## crsd (Nov 10, 2009)

hint: "1\nb"


----------



## crsd (Nov 10, 2009)

```
#!/bin/sh

clear
mc=0
IFS=',
'

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

IFS should include newline character.


----------



## GhettoBSD (Nov 10, 2009)

That did it. Should have asked yesterday!

Thank you very much!!


----------



## aragon (Nov 10, 2009)

I prefer this:


```
#!/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
```


----------

