Le Sun, Jan 12, 2025 at 04:10:03PM -0500, Chet Ramey a écrit :
> 
> This is a simplified version how it works: after you remove leading 
> and trailing IFS whitespace, you read individual fields from the input 
> using the characters in IFS as field terminators.

> If you get to the last variable and find a field terminator (in this 
> case, the end of input qualifies as a field terminator), you check 
> whether there is additional input.

> If there is, you just end the process there and assign whatever input 
> followed the previous delimiter you found to the last variable, 
> delimiters included. If not, the field is terminated and assigned to 
> the last variable.


Re-reading man page and this discussion, I thing either this variable is
wrongly named "IFS", as `S` stand for "separator", either the man page
paragraph regarding "IFS" is something light.

Btw, the command "mapfile" (readarray) seem more suitable** than "read" for
splitting an array on delimiter. (** constant)

   $ printf ' %-15s %-30s%s\n' String by\ Read{,Array};while IFS= read -r line;
       do
         IFS=: read -r _k _v1 _v2 <<<"$line"
         readarray -td: array < <(printf %s "$line");
         k="${array[0]}" v1="${array[1]}";
         IFS=:; v2=${array[*]:2}; IFS=$' \t\n';
         printf " %-15s k=%-3s v1=%-4s v2=%-11s  k=%-3s v1=%-4s v2=%-11s\n" \
                     "${line@Q}" "${_k@Q}" "${_v1@Q}" "${_v2@Q}" \
                     "${k@Q}" "${v1@Q}" "${v2@Q}";
     done < <(
        printf '%s\n' \
           k{:v1{:v2{:v3{:{:{:,},},},:{:{:,},},},:{:{:,},},},:{:{:,},},} )

    String          by Read                       by ReadArray
    'k:v1:v2:v3:::' k='k' v1='v1' v2='v2:v3:::'   k='k' v1='v1' v2='v2:v3::'  
    'k:v1:v2:v3::'  k='k' v1='v1' v2='v2:v3::'    k='k' v1='v1' v2='v2:v3:'   
    'k:v1:v2:v3:'   k='k' v1='v1' v2='v2:v3:'     k='k' v1='v1' v2='v2:v3'    
    'k:v1:v2:v3'    k='k' v1='v1' v2='v2:v3'      k='k' v1='v1' v2='v2:v3'    
    'k:v1:v2:::'    k='k' v1='v1' v2='v2:::'      k='k' v1='v1' v2='v2::'     
    'k:v1:v2::'     k='k' v1='v1' v2='v2::'       k='k' v1='v1' v2='v2:'      
    'k:v1:v2:'      k='k' v1='v1' v2='v2'         k='k' v1='v1' v2='v2'       
    'k:v1:v2'       k='k' v1='v1' v2='v2'         k='k' v1='v1' v2='v2'       
    'k:v1:::'       k='k' v1='v1' v2='::'         k='k' v1='v1' v2=':'        
    'k:v1::'        k='k' v1='v1' v2=''           k='k' v1='v1' v2=''         
    'k:v1:'         k='k' v1='v1' v2=''           k='k' v1='v1' v2=''         
    'k:v1'          k='k' v1='v1' v2=''           k='k' v1='v1' v2=''         
    'k:::'          k='k' v1=''   v2=''           k='k' v1=''   v2=''         
    'k::'           k='k' v1=''   v2=''           k='k' v1=''   v2=''         
    'k:'            k='k' v1=''   v2=''           k='k' v1=''   v2=''         
    'k'             k='k' v1=''   v2=''           k='k' v1=''   v2=''         








-- 
 Félix Hauri  -  <fe...@f-hauri.ch>  -  http://www.f-hauri.ch

Reply via email to