LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-14-2005, 12:25 PM   #1
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 927

Rep: Reputation: 30
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!
 
Old 04-14-2005, 01:37 PM   #2
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,617

Rep: Reputation: 136Reputation: 136
You could use IFS variable (Input File Separator) :
Code:
old=$IFS
IFS='
'
array=`cat Submissions`
for ...
    ...
done
IFS=$old
 
Old 04-14-2005, 01:52 PM   #3
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 927

Original Poster
Rep: Reputation: 30
Works perfectly, thanks! I would have never known about that otherwise.
 
Old 04-14-2005, 01:57 PM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
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
 
Old 04-14-2005, 02:16 PM   #5
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 927

Original Poster
Rep: Reputation: 30
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?
 
Old 04-14-2005, 02:24 PM   #6
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,617

Rep: Reputation: 136Reputation: 136
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
 
Old 04-14-2005, 02:55 PM   #7
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
how big is this 'Submissions' file? do you even need to worry about memory? and can you even tell a difference in speed?
 
Old 04-14-2005, 02:58 PM   #8
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 927

Original Poster
Rep: Reputation: 30
~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.

Last edited by rose_bud4201; 04-14-2005 at 04:02 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash: Supress newlines with \c - Doesn't work stefanlasiewski Programming 5 07-26-2005 02:17 PM
split string prob izza_azhar Programming 3 02-08-2005 01:11 AM
split string izza_azhar Programming 6 01-18-2005 09:24 PM
get file contents with newlines into bash variable otoomet Linux - Software 2 01-06-2005 02:23 PM
Need to split an input string general4172 Linux - Software 6 10-30-2003 12:57 AM


All times are GMT -5. The time now is 08:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration