3

Coming from this answer I'm trying to output a sequence of numbers using a dot as a decimal separator.

This works:

$ LANG=en_US seq 0.1 0.1 0.8
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8

$ LANG=en_US seq 0.1 0.1 1.0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0

But this doesn't:

$ LANG=en_US seq 0.1 0.1 0.9
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0,9

Why? How can I fix it?

nistel
  • 113
  • 7
  • It is giving me correct result !! Which version of seq are you using ? – Rahul Verma Oct 11 '17 at 20:52
  • `seq` is not part of bash -- it's an external tool your OS vendor may or may not supply, and because it's not POSIX-standardized, there are no guarantees about which behavior it should or shouldn't implement. Maybe this should be tagged `unix`? – Charles Duffy Oct 11 '17 at 20:52
  • (Also, because it's provided by your OS vendor, you really need to specify that OS in the question to give folks a reasonable chance at being able to reproduce the behavior at hand). – Charles Duffy Oct 11 '17 at 20:53
  • 1
    BTW, what behavior do you get setting `LC_ALL=C` rather than `LANG=en_US`? If that fixes things, you'll want to look into non-`LANG` locale variables. – Charles Duffy Oct 11 '17 at 20:54
  • @batMan I'm not sure how to figure out the version of seq. `seq --help` does not yield this information, but `man seq` says `GNU coreutils 8.25` on the bottom left. – nistel Oct 11 '17 at 20:55
  • 1
    @CharlesDuffy `LC_ALL=C` fixes this. Thanks! – nistel Oct 11 '17 at 20:57
  • use `seq --version` – Rahul Verma Oct 11 '17 at 20:57

2 Answers2

3

To prevent any locale settings (such as LC_NUMERIC, a likely culprit here) from influencing behavior:

LC_ALL=C seq 0.1 0.1 0.9

That said, I don't advise using seq at all. It's a nonstandard command, not guaranteed to be available on all UNIX platforms or to have any specific behavior when it is available. An a floating-point-capable alternative, consider awk:

LC_ALL=C awk -v min=0.1 -v max=0.9 -v inc=0.1 \
  'BEGIN { cur=min; while (cur <= max) { print cur; cur += inc; }; exit; }'
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
1

It's probably because of the LANG variable that sets locale setting and causing seq to behave like this. It's not clear why but to fix it EXPLICITLY you can us tr. For eg.

$ LANG=en_US seq 0.1 0.1 0.9 | tr "," "."
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Rahul Verma
  • 2,946
  • 14
  • 27
  • BTW, re: reasoning behind my edit -- see [When should code formatting be used for non-code text?](https://meta.stackoverflow.com/questions/254990/when-should-code-formatting-be-used-for-non-code-text) on meta; to quote the consensus answer, "code formatting shouldn't be used for emphasis on regular words". – Charles Duffy Oct 11 '17 at 21:05
  • @CharlesDuffy : I'll make a note of this Charles. Thanks for this link. – Rahul Verma Oct 11 '17 at 21:07