Q: ksh - how to handle strings with spaces

Q: ksh - how to handle strings with spaces

Post by Kim B?ndergaard Poulse » Fri, 06 Mar 1998 04:00:00



Hi

I've looked in the FAQ but not been able to find anything.

Hope some one is able to help me.

My problem is that I have a file, say /etc/passwd, consisting of lines each
with a number of fields separated by ':'

Each field is a string where spaces are allowed. In /etc/passwd e.g. the
description field  as here in this example:

lp:x:71:8:Line Printer Admin:/usr/spool/lp:

How can I in ksh make a loop reading whole lines from the file.

Doing the following results in 3 parts of the line shown above.

Example
$ ksh
$ FILE=/tmp/xx
$ cat $FILE
lp:x:71:8:Line Printer Admin:/usr/spool/lp:
$ for LINE in `cat $FILE`

Quote:> do
> echo $LINE
> done

lp:x:71:8:Line
Printer
Admin:/usr/spool/lp:
$

In csh it is possible to surround the cat with double quotes as in the
following:
$csh
bluto % foreach LINE ("`cat $FILE`" )
foreach? echo $LINE
foreach? end
lp:x:71:8:Line Printer Admin:/usr/spool/lp:
bluto %

/Kim

 
 
 

Q: ksh - how to handle strings with spaces

Post by Kim B?ndergaard Poulse » Fri, 06 Mar 1998 04:00:00


I've found a solution myself.

ksh has an internal parameter called IFS (Internal Field Separators) which
by default is set to space, tab and newline.

By setting it to newline only my problem is solved.

It can be (saved and) set to newline by

OLD_IFS=$IFS
IFS="
"

/Kim



Quote:> Hi

> I've looked in the FAQ but not been able to find anything.

> Hope some one is able to help me.

> My problem is that I have a file, say /etc/passwd, consisting of lines
each
> with a number of fields separated by ':'

> Each field is a string where spaces are allowed. In /etc/passwd e.g. the
> description field  as here in this example:

> lp:x:71:8:Line Printer Admin:/usr/spool/lp:

> How can I in ksh make a loop reading whole lines from the file.

> Doing the following results in 3 parts of the line shown above.

> Example
> $ ksh
> $ FILE=/tmp/xx
> $ cat $FILE
> lp:x:71:8:Line Printer Admin:/usr/spool/lp:
> $ for LINE in `cat $FILE`
> > do
> > echo $LINE
> > done
> lp:x:71:8:Line
> Printer
> Admin:/usr/spool/lp:
> $

> In csh it is possible to surround the cat with double quotes as in the
> following:
> $csh
> bluto % foreach LINE ("`cat $FILE`" )
> foreach? echo $LINE
> foreach? end
> lp:x:71:8:Line Printer Admin:/usr/spool/lp:
> bluto %

> /Kim


 
 
 

1. ksh: convert space delimited string to array, but with '\ ' option

I'm trying to convert a space delimeted string to an array, but allow for
spaces in array elements by escaping with '\ '.

It seems to work manually...
# set -A array b\ cd efg
# print ${array[0]}
b cd

...but not from a variable
# input='b\ cd efg'
# set -A array $input
# print ${array[0]}
b\

How can I get around this?  Thanks.

--
Regards,
Chris Sites

2. SunOS / Linux Object Files

3. Multiple spaces within a string? (ksh)

4. Security Vulnerability in rpc.ttdbserver (Rev.2)

5. Riddle me this - when is a ksh string not a string?

6. divx in noatun

7. Qs on ksh parameter expansion (BUG?)

8. Answers to your Questions

9. String handling in sh

10. in 'getopt()', how to handle multiple options in the 'optarg' string?

11. sparse tool handling of escaped characters in strings

12. DBCS/MBCS aware string handling functions

13. Q: Can dip handle complex strings ?