[lug] scripting with spaces in strings

Chip Atkinson chip at pupman.com
Thu Oct 14 08:42:55 MDT 2004


Thanks for the suggestion!  It worked like a charm.  I kind of toyed with
IFS but did something slightly different and ended up off in the weeds.

Chip

On Wed, 13 Oct 2004, Zan Lynx wrote:

> On Wed, 2004-10-13 at 16:41 -0600, Chip Atkinson wrote:
> > Greetings,
> > 
> > I have a file containing file names with spaces in them and want to loop
> > through it and have the possibly space containing file names treated as a
> > single item.
> > 
> > For example, I have a file containing
> > a b c d
> > e f g
> > hijk
> > l m n o
> > 
> > I've tried 
> > for i in $(grep [a-z] junkdat | sed -e 's/ /\\ /'); do
> > > echo $i
> > > done
> >  
> > and 
> > 
> > for i in $(echo '"'$(grep [a-z] junkdat | sed -e 's/
> > /\\ /')'"'); do
> > > echo $i
> > > done
> > 
> > I basically get the following output:
> > 
> > "a\
> > b
> > c
> > d
> > e\
> > f
> > g
> > hijk
> > l\
> > m
> > n
> > o"
> > 
> > 
> > What I'd like would be
> > 
> > a\ b\ c\ d
> > e\ f\ g
> > hijk
> > l\ m\ n\ o
> > 
> > Any suggestions/thoughts?  I'm puzzled.
> > 
> > Thanks!
> > 
> > Chip
> 
> Try:
> IFS=$'\n'
> for i in $(cat junkdat); do echo "$i"; done
> 
> and go from there.
> -- 
> Zan Lynx <zlynx at acm.org>
> 




More information about the LUG mailing list