Operating System - HP-UX
1753797 Members
7231 Online
108802 Solutions
New Discussion

ksh: using of IFS ( check correct number of fields )

 
support_billa
Valued Contributor

ksh: using of IFS ( check correct number of fields )

hello,

 

i many scripts we are using IFS to parse files with a loop. if the number of fields are correct, it is a powerful tool.

can i  check the number of fields, before the loop starts ?

 

IFS : #  .

 

example of parse 3 lines , line 3 has to many fields . also we have the issue, in ksh88 it is no problem, when to many fields exists ( the last fields were cutted ) and in ksh93 all supernumerous values (or fields) are in the last field.

i think this behaviour is correct .

 

example :

 

#!/usr/bin/ksh

echo "line1:field1#field2#field3##
line2:field1#field2#field3##
line3:field1#field2#field3# ##" | \
while IFS=# read field1 field2 field3
do
 echo "field1=${field1} field2=${field2} field3=${field3}"
done

 

output:

 

field1=line1:field1 field2=field2 field3=field3
field1=line2:field1 field2=field2 field3=field3
field1=line3:field1 field2=field2 field3=field3#

 

regards

 

1 REPLY 1
Dennis Handly
Acclaimed Contributor

Re: ksh: using of IFS and read (check correct number of fields)

>in ksh88 it is no problem, when too many fields exists (the last fields were cut)

 

No, read puts them in the last field.

 

>field1=line1:field1 field2=field2 field3=field3

 

There are only three fields with trailing separators

 

>field1=line3:field1 field2=field2 field3=field3#

 

There are 4 fields.  The separators and the 4th field are concatenated to field3.  Any trailing separators are discarded.

 

I see no differences between dtksh  and ksh88.