quick Korn question on reading input from a comma delimited file:

quick Korn question on reading input from a comma delimited file:

Post by Stev » Thu, 18 Apr 2002 01:43:25



Is there a way to count the number of columns on a line to make sure
there are enough fields?

for instance:
$ cat test.csv
cat,dog
cow,turtle,lion,dove
snake,bird,snake,dragon,tiger

I want to make sure each line has only three (3) variables.

#!/usr/bin/ksh
# 4/15/2002

IFS=,
exec 0<$1
while read -r variableA variableB variableC
do
# somehow check to see if each line has 3 fields before continuing.
print $variableA
print $variableB
print $variableC
done
exit

 
 
 

quick Korn question on reading input from a comma delimited file:

Post by Chris F.A. Johnso » Thu, 18 Apr 2002 02:01:01



> Is there a way to count the number of columns on a line to make sure
> there are enough fields?

> for instance:
> $ cat test.csv
> cat,dog
> cow,turtle,lion,dove
> snake,bird,snake,dragon,tiger

> I want to make sure each line has only three (3) variables.

> #!/usr/bin/ksh
> # 4/15/2002

> IFS=,
> exec 0<$1
> while read -r variableA variableB variableC
> do
> # somehow check to see if each line has 3 fields before continuing.
> print $variableA
> print $variableB
> print $variableC
> done
> exit

        while IFS=, read variableA variableB variableC extra
        do
            if [ -z "$extra" ]
            then
               print $variableA
               print $variableB
               print $variableC
            else
               echo "$line has too many fields" >&2
            fi
        done < $1

--

        =================================================================



 
 
 

quick Korn question on reading input from a comma delimited file:

Post by Barry Margoli » Thu, 18 Apr 2002 01:58:08




>Is there a way to count the number of columns on a line to make sure
>there are enough fields?

>for instance:
>$ cat test.csv
>cat,dog
>cow,turtle,lion,dove
>snake,bird,snake,dragon,tiger

>I want to make sure each line has only three (3) variables.

>#!/usr/bin/ksh
># 4/15/2002

>IFS=,
>exec 0<$1
>while read -r variableA variableB variableC
>do
># somehow check to see if each line has 3 fields before continuing.

case "$variableC" in
 "") print "Not enough fields"
     exit 1 ;;
 *,*) print "Too many fields"
     exit 2 ;;
esac

Quote:>print $variableA
>print $variableB
>print $variableC
>done
>exit

--

Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
 
 
 

quick Korn question on reading input from a comma delimited file:

Post by Jon LaBadi » Fri, 19 Apr 2002 10:29:37



> Is there a way to count the number of columns on a line to make sure
> there are enough fields?

> for instance:
> $ cat test.csv
> cat,dog
> cow,turtle,lion,dove
> snake,bird,snake,dragon,tiger

> I want to make sure each line has only three (3) variables.

> #!/usr/bin/ksh
> # 4/15/2002

> IFS=,
> exec 0<$1
> while read -r variableA variableB variableC
> do
> # somehow check to see if each line has 3 fields before continuing.
> print $variableA
> print $variableB
> print $variableC
> done
> exit

how about a function to output the number of fields?

function commafields
{
         typeset array
         IFS=,

         set -A array x${*}x
         echo ${#array[*]}

Quote:}

call it as "commafields <entireline>"
 
 
 

quick Korn question on reading input from a comma delimited file:

Post by David Thompso » Fri, 19 Apr 2002 18:56:55



Quote:> function commafields
> {
>          typeset array

You should probably save IFS first,

           OLDIFS="$IFS"

before you change it,

Quote:>          IFS=,
>          set -A array x${*}x

and then restore it,

           IFS="$OLDIFS"

when your done.

Quote:>          echo ${#array[*]}
> }

--
David Thompson

 
 
 

quick Korn question on reading input from a comma delimited file:

Post by laura fairhe » Fri, 19 Apr 2002 21:33:34




>> function commafields
>> {
>>          typeset array

>You should probably save IFS first,

>           OLDIFS="$IFS"

>before you change it,

>>          IFS=,
>>          set -A array x${*}x

>and then restore it,

>           IFS="$OLDIFS"

>when your done.

>>          echo ${#array[*]}
>> }

This has been bugging me all week, namely; what if IFS is not set?

In theory under a POSIX system $IFS could be 'undef' (not set)
which is valid and the shell must default to behaving as if
it was <space> <tab> <newline>;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 3.5.3  Variables

    IFS                Input field separators:  a string treated as a list
                       of characters that is used for field splitting and
                       to split lines into fields with the read command.
                       If IFS is not set, the shell shall behave as if the
                       value of IFS were the <space>, <tab>, and <newline>
                       characters.  (See 3.6.5.)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If you use the sequence;

OIFS=$IFS
IFS=whatever
# dosomething
IFS=$OIFS

... and IFS was initially <undef> it will become nul afterwards.
Behaviour for a nul IFS is not the same as for an unset one;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 3.6.5  Field Splitting

 After parameter expansion (3.6.2), command substitution (3.6.3), and
 arithmetic expansion (3.6.4) the shell shall scan the results of
 expansions and substitutions that did not occur in double-quotes for
 field splitting and multiple fields can result.

 The shell shall treat each character of the IFS as a delimiter and use
 the delimiters to split the results of parameter expansion and command
 substitution into fields.

     (1)  If the value of IFS is <space>, <tab>, and <newline>, or if it
          is unset, any sequence of <space>, <tab>, or <newline>
          characters at the beginning or end of the input shall be ignored
          and any sequence of those characters within the input shall
          delimit a field.  (For example, the input

                <newline><space><tab>foo<tab><tab>bar<space>

          yields two fields, foo and bar).

     (2)  If the value of IFS is null, no field splitting shall be
          performed.

     (3)  Otherwise, the following rules shall be applied in sequence.      1
          The term ``IFS white space'' is used to mean any sequence (zero   1
          or more instances) of white-space characters that are in the IFS  1
          value (e.g., if IFS contains <space><comma><tab>, any sequence    1
          of <space> and <tab> characters is considered IFS white space).   1

           (a)  IFS white space shall be ignored at the beginning and end   1
                of the input.                                               1

           (b)  Each occurrence in the input of an IFS character that is    1
                not IFS white space, along with any adjacent IFS white      1
                space, shall delimit a field, as described previously.      1

           (c)  Nonzero-length IFS white space shall delimit a field.       1

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The only way I can see so far to work around this in a clean looking way
is by doing everything in a sub-shell;

(
IFS=whatever

# do something
)

Maybe there is a reasonable way to do it in the same shell, without
having to resort to a whole lot of code (I can only see a 4 line solution
at the moment...)

>--
>David Thompson


bestwishes

--

                # if you are bored crack my sig.
1F8B0808CABB793C0000666667002D8E410E83300C04EF91F2877D00CA138A7A
EAA98F30C494480157B623C4EF1B508FDED1CEFA9152A23DE35D661593C5318E
630C313CD701BE92E390563326EE17A3CA818F5266E4C2461547F1F5267659CA
8EE2092F76C329ED02CA430C5373CC62FF94BAC6210B36D9F9BC4AB53378D978
80F2978A1A6E5D6F5133B67B6113178DC1059526698AFE5C17A5187E7D930492

 
 
 

quick Korn question on reading input from a comma delimited file:

Post by Dan Merc » Sat, 20 Apr 2002 01:48:53





>> function commafields
>> {
>>          typeset array

> You should probably save IFS first,

>            OLDIFS="$IFS"

> before you change it,

>>          IFS=,
>>          set -A array x${*}x

> and then restore it,

>            IFS="$OLDIFS"

> when your done.

>>          echo ${#array[*]}
>> }

> --
> David Thompson


If you are going to use a Korn function,  just typeset IFS and
it will be local to the function.

--
Dan Mercer

Opinions expressed herein are my own and may not represent those of my employer.

 
 
 

quick Korn question on reading input from a comma delimited file:

Post by Alvin SI » Tue, 30 Apr 2002 13:01:33


Hi,

Usually I will use the following:

        while read -r variableA variableB variableC variableCOMMENT

to take away the 4th variable and onward.

Alvin SIU



>> Is there a way to count the number of columns on a line to make sure
>> there are enough fields?

>> for instance:
>> $ cat test.csv
>> cat,dog
>> cow,turtle,lion,dove
>> snake,bird,snake,dragon,tiger

>> I want to make sure each line has only three (3) variables.

>> #!/usr/bin/ksh
>> # 4/15/2002

>> IFS=,
>> exec 0<$1
>> while read -r variableA variableB variableC
>> do
>> # somehow check to see if each line has 3 fields before continuing.
>> print $variableA
>> print $variableB
>> print $variableC
>> done
>> exit

> how about a function to output the number of fields?

> function commafields
> {
>         typeset array
>         IFS=,

>         set -A array x${*}x
>         echo ${#array[*]}
> }

> call it as "commafields <entireline>"

 
 
 

1. reading in comma delimited variables in korn

I have the following mail program written in korn shell.  I need to
change it so, instead of reading in a line and sending it out to that
email address, I need it to take a second value and place it in the
body.  How would I change my below program to do that, placing the
number where I have the $num.  Thanks.  So for example, the data would
be:



--snip--

#!/bin/ksh

TODAY=`date +%a`
base=/var/home/cpws
maildir=$base/email
todaymail=$maildir/$TODAY

if [[ -r $todaymail ]]; then
  exec 3< "$todaymail"
else
  exit
fi

while read -u3 aline; do
  {       ## start subprocess to create message

    echo "To: $aline"
    echo "Subject: Heres your ID"
    echo ""
    echo Your number is: $num
  }  | /usr/lib/sendmail -t -oi
  sleep 2

done

--snip--

2. ppp w/ -auto -alias

3. Need help on creating comma delimited file

4. 802.11b USB network adapter drivers

5. FORM CGI Available?-- to export comma delimited data to file

6. Help improving the vgalib???

7. matching records in a comma delimited file

8. WHEN will NCSA Mosaic correct TABLE problems?

9. Comma delimited file

10. Sorting files with comma delimited fields

11. csplit to parse comma delimited file

12. dispatching data delimited with comma into a new line

13. How can I make lines comma delimited