LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 06-10-2008, 10:45 PM   #1
babag
Member
 
Registered: Aug 2003
Posts: 419

Rep: Reputation: 31
bash_-_new line separator?


having a problem with a bash script in which i'm reading
from a text file.

Code:
i=0
ii=61
IFS="
"
#IFS=$'\n'

pathis="path/to/file"

for line in `cat $pathis/ScriptVariables.txt`;
     do
          ScriptVariables[$i]="$line"
          let i++
     done
this file, ScriptVariables.txt, has many lines,
some of which are empty.

my problem is that, when the file is read into
my script, the empty lines are skipped over. i
need to have these lines read, even if the value
is null.

how do i get this script to include the empty
lines?

as you can see, i've tried two differing methods
for declaring the IFS but they both are behaving
the same way: skipping empty lines.

thanks,
BabaG

Last edited by babag; 06-10-2008 at 10:47 PM.
 
Old 06-11-2008, 02:48 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
In general, this kind of loop is not a good idea:
Code:
for line in `cat $pathis/ScriptVariables.txt`;
. Because:
  • Uses more memory (more lines, more memory up to the point where bash even may choke in it)
  • slow (every extra line slows down more)
  • lines with spaces in will be seen as seperate lines. (OTOH if that is what you need..., but even then there are better ways)
  • empty lines are skipped (your problem).

Two ways fix the above problems:
Code:
#!/bin/bash

i=1
cat /tmp/lines.txt | while read LINE ; do
    if [ "$LINE" == "" ] ; then
        echo "===EMPTY LINE==="
    else
        echo "#$i : $LINE"
    fi
    let i++
done
or:
Code:
#!/bin/bash

i=1
while read LINE ; do
    if [ "$LINE" == "" ] ; then
        echo "===EMPTY LINE==="
    else
        echo "#$i : $LINE"
    fi
    let i++
done </tmp/lines.txt
 
Old 06-11-2008, 11:55 AM   #3
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
With the while read loops above, you will probably want to temporarily change IFS as well (otherwise, leading spaces or tabs will be swallowed up).
 
Old 06-11-2008, 01:01 PM   #4
babag
Member
 
Registered: Aug 2003
Posts: 419

Original Poster
Rep: Reputation: 31
thanks. this is getting me there. tried this:
Code:
i=0
ii=160
IFS="
"

pathis="/home/babag/Desktop/To_LOM"

cat $pathis/ScriptVariables.txt | while read LINE ; 
do
    if [ "$LINE" == "" ] ; then
        echo "===EMPTY LINE==="
        ScriptVariables[$i]="===EMPTY LINE==="
    else
        echo "#$i : $LINE"
        ScriptVariables[$i]="$LINE"
    fi
    let i++
done

echo ${ScriptVariables[112]}
just added the ScriptVariables[$i]="$LINE" stuff to try to get
values stored. when i try the
Code:
echo ${ScriptVariables[$112]}
bit at the end, though, i get nothing. did i muck up the syntax
on that or am i assigning the array wrongly?

thanks again,
BabaG
 
Old 06-11-2008, 01:11 PM   #5
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Try using redirection instead of the pipe (otherwise a subshell is started)
 
Old 06-11-2008, 01:16 PM   #6
babag
Member
 
Registered: Aug 2003
Posts: 419

Original Poster
Rep: Reputation: 31
edit:

switched to second example from Hko and seem to be able
to get that one to work. presumably that's because it
doesn't use the pipe as you point out. would still love
to see an example of how to rewrite the piped line using
redirection, though.


thanks osor. kind of dumb here. example please?

we're talking about this line?
Code:
cat $pathis/ScriptVariables.txt | while read LINE ;
thanks again,
BabaG

Last edited by babag; 06-11-2008 at 01:42 PM.
 
Old 06-11-2008, 03:26 PM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Hko's second example is the way to rewrite with redirection.

by the way, you could also do
Code:
IFS="
"
ScriptVariables=($(sed 's/^$/===EMPTY LINE===/' $pathis/ScriptVariables.txt))
echo ${ScriptVariables[112]}
This has the efficiency issues Hko mentioned, but if you're putting the whole file into an array anyways I assume it's small...
 
Old 06-11-2008, 03:46 PM   #8
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by babag View Post
would still love
to see an example of how to rewrite the piped line using
redirection, though.


thanks osor. kind of dumb here. example please?
When you have a pipeline, each part of the pipeline is executed in a subshell. So any variables which are created or modified in that subshell go out of scope at the end of the subshell.

If you want pipe-like behavior with redirection, some of the newer shells (e.g., bash) allow process substitution to a temporary file (/dev/fd/number).

So in bash, when you type
Code:
<(process)
a temporary file is created (in memory) with the contents of the output of process. You can now redirect from this temporary file:
Code:
< <(process)
Btw, if you are putting all lines of a file or command into an array, see this thread.
 
Old 06-12-2008, 02:11 PM   #9
babag
Member
 
Registered: Aug 2003
Posts: 419

Original Poster
Rep: Reputation: 31
thanks osor! and for the link as well.

BabaG
 
  


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
Decimal separator morfeus80 Linux - Newbie 2 04-21-2008 04:32 AM
Bash command separator/arguments separator reverse Programming 11 12-02-2007 09:21 PM
make separator error robw Linux - Newbie 0 12-06-2003 02:49 PM
Makefile separator problem Ownasaurus Linux - Software 5 09-07-2003 01:07 PM
Missing Separator? jamespetts Linux - Software 2 09-05-2002 12:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:12 AM.

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
Open Source Consulting | Domain Registration