Split a string on newlines (bash)
Hi all,
I'm trying to write a [really] simple bash script that reads in a series of words, one per line, from a file "Submissions" and loads them into an array which I can then iterate through. So far the script looks mainly like Code:
rawfile=`cat Submissions` Thanks! |
You could use IFS variable (Input File Separator) :
Code:
old=$IFS |
Works perfectly, thanks! I would have never known about that otherwise.
|
Do your really need to store it in an array?
If not, you can do it faster and saving some memory: Code:
#!/bin/bash |
Hmm. Well, I suppose that I don't, really. Doing it that way, how does one open the file so that it knows to read one token per line? Does it default to reading a line at a time?
I assume that the "<Submissions" has something to do with it? |
the read command stores input in a variable until the enter or return key is pressed
so here the read command store input from file Submissions (<Submissions) and stop to store in subid variable each time it reads a end of line (as a return or enter key) Basically the code posted by hko should use less memory as it doesn't store all the Submission lines before work with it, instead it stores line per line in memory |
how big is this 'Submissions' file? do you even need to worry about memory? and can you even tell a difference in speed?
|
~7000 lines, probably not, probably not :)
And re: the read command explanation - thanks! I'm learning a lot during the course of just this one task, and every little bit is helpful. |
All times are GMT -5. The time now is 06:57 PM. |