Hacker News new | ask | show | jobs
by pizza234 1787 days ago
> The extra overhead and impact on runtime of context switching to load in a new program is non-trivial if you have to loop over it thousands of times

The specification is: "speed does not matter".

The long answer addresses this solution:

  $ string="Los Angeles, London, Belfast, New York"
  $ IFS="," read -r -a array <<< "${string/, /,}"
  
  $ echo ${array[0]}
  Los Angeles
  
  $ echo ${array[1]}
  London
as "not very generic" in point #3, which is correct. Bash simply doesn't support generic splitting by itself (things go downhill quickly once, for example, newlines are introduced, and so on), and if precision/flexibility are priority over speed, then it's better to use standard linux tools.
1 comments

If you have newlines present then process the data a line at a time, as you would if reading from a file. This is nowhere near as difficult or cumbersome as you're making out.
One certainly can, but the increase in complexity shows that Bash starts not to be the most effective tool, when performing tasks it isn't designed for (and compare to a full-blown programming language, there are many).