script to read a line in an array with , as IFS

script to read a line in an array with , as IFS

Post by Vino » Thu, 10 Apr 2003 06:29:25



Guys
line is - temp123,test123,abc123
i need to build a array from this line (IFS=,)
testarray[0]=temp123
testarray[1]=test123
testarray[2]=abc123

following is the script i wrote...i dont know the reason why its not wrong

while IFS=',' read server; do
        i=$((i+1));
        ${testarray[i]}="$server"
done < filename

can someone help me with this......

thanks & regards
Vinod

 
 
 

script to read a line in an array with , as IFS

Post by Lurc » Thu, 10 Apr 2003 06:49:41



> Guys
> line is - temp123,test123,abc123
> i need to build a array from this line (IFS=,)
> testarray[0]=temp123
> testarray[1]=test123
> testarray[2]=abc123

> following is the script i wrote...i dont know the reason why its not wrong

> while IFS=',' read server; do
>         i=$((i+1));
>         ${testarray[i]}="$server"
> done < filename

in bash it would be as simple as:

while read SERVER; do
        TESTARRAY=(${SERVER//,/ })
done < filename

echo "El 1: ${TESTARRAY[0]}"
echo "El 2: ${TESTARRAY[1]}"
echo "El 3: ${TESTARRAY[2]}"

I'm not sure how portable this code is though (probably not very :P)
Messing around with IFS is error-prone IMO. If you want to do it, make sure
to save the old IFS, and revert back to it after you've finished with it.
(or do it all in a subshell. That way the new IFS will be destroyed when
the subshell exits) As for your problem, I'm not sure how to use IFS here..

HTH,
Lurch_

 
 
 

script to read a line in an array with , as IFS

Post by Greg Andre » Thu, 10 Apr 2003 06:56:23



>Guys
>line is - temp123,test123,abc123
>i need to build a array from this line (IFS=,)
>testarray[0]=temp123
>testarray[1]=test123
>testarray[2]=abc123

>following is the script i wrote...i dont know the reason why its not wrong

>while IFS=',' read server; do
>        i=$((i+1));
>        ${testarray[i]}="$server"
>done < filename

Here's what I did in Solaris' ksh88:

    #!/bin/ksh

    # remember my original $IFS
    OLDIFS="${IFS}"

    while read line ; do

        # split line on ',' into testarray[]
        IFS=',' set -A testarray $line

        # restore original $IFS for processing
        IFS="$OLDIFS"

        # process the testarray[] here

    done < testfile

  -Greg
--

     I have a map of the United States that's actual size.
                                -- Steven Wright

 
 
 

script to read a line in an array with , as IFS

Post by Kevin Rodger » Thu, 10 Apr 2003 06:59:41



> line is - temp123,test123,abc123
> i need to build a array from this line (IFS=,)
> testarray[0]=temp123
> testarray[1]=test123
> testarray[2]=abc123

> following is the script i wrote...i dont know the reason why its not wrong

> while IFS=',' read server; do
>         i=$((i+1));
>         ${testarray[i]}="$server"
> done < filename

The problem is that IFS is a field separator, not a record separator: it
determines how a line is split into words, but the read built-in reads a
line at a time.  Try this instead:

tr , '\n' < filename | while read server; do
        ...
done

--

 
 
 

script to read a line in an array with , as IFS

Post by Stephane CHAZELA » Thu, 10 Apr 2003 07:03:15



> line is - temp123,test123,abc123
> i need to build a array from this line (IFS=,)
> testarray[0]=temp123
> testarray[1]=test123
> testarray[2]=abc123

> following is the script i wrote...i dont know the reason why its not wrong

> while IFS=',' read server; do
>         i=$((i+1));
>         ${testarray[i]}="$server"
> done < filename

ksh93, bash, zsh:

IFS=',
'
testarray=( $(cat filename) )

ksh88:

IFS=',
' set -A testarray -- $(cat filename)

--
Stphane

 
 
 

script to read a line in an array with , as IFS

Post by William Par » Thu, 10 Apr 2003 16:08:08



> Guys
> line is - temp123,test123,abc123
> i need to build a array from this line (IFS=,)
> testarray[0]=temp123
> testarray[1]=test123
> testarray[2]=abc123

> following is the script i wrote...i dont know the reason why its not wrong

> while IFS=',' read server; do
>        i=$((i+1));
>        ${testarray[i]}="$server"
> done < filename

> can someone help me with this......

1.  while IFS=, read -a testarray; do
        ...
    done < filename

2.  testarray=( `IFS=, echo $server` )

3.  IFS=, testarray=( $server )

4.  testarray=()
    array -j ',' testarray "$server"

--

Linux solution for data management and processing.

 
 
 

script to read a line in an array with , as IFS

Post by Stephane CHAZELA » Thu, 10 Apr 2003 17:11:55


[...]
Quote:> 3.  IFS=, testarray=( $server )

[...]

Note that those are two assignents. So IFS will remain ",", and
assignments are not required to be evaluated left to right.
Historically, in Bourne shell, they were evaluated right to
left.

IFS=,
testarray=( $server )

is the same and is safer.

--
Stphane

 
 
 

script to read a line in an array with , as IFS

Post by Vino » Sun, 13 Apr 2003 04:13:06


Thanks Guys

Used this for ksh --

IFS=','
set -A serverarray -- $var4