[lug] scripting with spaces in strings

Zan Lynx zlynx at acm.org
Wed Oct 13 16:46:13 MDT 2004


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>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
URL: <http://lists.lug.boulder.co.us/pipermail/lug/attachments/20041013/72c2f1d8/attachment.pgp>


More information about the LUG mailing list