LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Split a string on newlines (bash) (https://www.linuxquestions.org/questions/programming-9/split-a-string-on-newlines-bash-313206/)

rose_bud4201 04-14-2005 11:25 AM

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`
array=__do something to $rawfile, possibly involving magic__

for subid in $array; do
  x=`db2 "select count(*) from table where uniquekey='$subid'"
  if [ "x" == "0" ]; then
        echo "No submissions here!"
  elif [ "x" == SQL* ]; then
        echo "Fix your SQL, silly."
  fi
done

I've seen a website which advocates using awk, but the example given is a hard-coded string and only has 2 entities in it when split, so I can't tell if what's done there is what I'm supposed to be doing here. It certainly didn't work when I tried it :( Does anyone have a good way to do this?

Thanks!

keefaz 04-14-2005 12:37 PM

You could use IFS variable (Input File Separator) :
Code:

old=$IFS
IFS='
'
array=`cat Submissions`
for ...
    ...
done
IFS=$old


rose_bud4201 04-14-2005 12:52 PM

Works perfectly, thanks! I would have never known about that otherwise.

Hko 04-14-2005 12:57 PM

Do your really need to store it in an array?
If not, you can do it faster and saving some memory:
Code:

#!/bin/bash

while read subid ; do
  x=`db2 "select count(*) from table where uniquekey='$subid'"
  if [ "x" == "0" ]; then
        echo "No submissions here!"
  elif [ "x" == SQL* ]; then
        echo "Fix your SQL, silly."
  fi
done <Submissions


rose_bud4201 04-14-2005 01:16 PM

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?

keefaz 04-14-2005 01:24 PM

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

sirclif 04-14-2005 01:55 PM

how big is this 'Submissions' file? do you even need to worry about memory? and can you even tell a difference in speed?

rose_bud4201 04-14-2005 01:58 PM

~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.