2011w49

IFS and for loops

I needed to iterate over lines in a file, and I needed to use a for loop (well, I probably could have solved it in a myriad other ways, but that’s not the point).

Thanks Luke, updating for clarification: I simplified this problem somewhat to make the post shorter, but the problem in need of solving involved doing some addition across lines, and have the result available after the loop, and for this, I have learned, pipes and the “while read line; do”-pattern isn’t of much help.

So I tell the for loop to do

for line in `cat myfile`; do echo $lines; done

And obviously this doesn’t work, as the IFS variable is set to space, and thus prints out each word on a separate line, instead of printing all the words in one line on lines.

So I think “oh I know, I’ll just change the IFS variable” and try:

IFS="\n"

and this turns out poorly, with the for loop now believing every “n” (and “\”, thanks Luke :)) to be the separator and breaking words on that instead… So I try with single quotes, no joy…

Having approached and passed the point where it is taking me more time to solve this problem rather than solving the problem I was using the loop for, I stop trying and start googling, finding this post.

The solution is rather nifty actually:

IFS='
'

There you have it.

Cube

I haven’t tried it out, but this seems like it could be useful. From that page one could also make their way to one of the projects powering Cube, namely D3, and on that page you can find one or two (or more) interesting diagram types.

And filed under “oh frak! how glad I am that I never got a paypal account!”:

  1. http://www.regretsy.com/2011/12/05/cats-1-kids-0/
  2. https://www.thepaypalblog.com/2011/12/regretsy-issue-resolution/
  3. http://thegreengeeks.wordpress.com/2011/12/06/why-paypal-is-wrong-regarding-regretsy-according-to-their-own-policies/

:wq

Tags: , , , , ,

2 Responses to “2011w49”

  1. The importance of this is of course playing with IFS, not the loop itself, but this situation is more suited to a while loop.

    cat myfile | while read line; do echo $line; done

    By the way, it setting IFS=”\n” doesn’t make it just separate on “n”, but also “\”.

  2. Patrik says:

    Hey Luke :)

    You are absolutely correct, the problem I described should be solved with a while-loop.

    The actual problem I was solving (and which I “dumbed” down too much to keep the post relatively short) involved counting the number of words on each line (the goal was to do a quick and dirty averaging of the number of words per line in the file) which made the “cat myfile | while read line” unsuitable (hence my use of the for-loop).

    Of course now that I think about it, it would have been much easier to just do a wc -c / wc -l

    Ah! I never reflected upon that it would also break on “\” (mostly because I didn’t have any in my output). I’ll update that. Thanks :)