Delimited File to 2D Array

Hello,

I need help in fetching data from a delimited file , into a 2D array.

Sample Input File:

"|" as delimiter

A|123|446pr;
B|46|hello89
krp;
C|78|ystp90
67;
D|ga|456;

Please be advised that ";" is the line separator (not "\n"). Could you please write an awk script to fetch this into a 2D array.

Output: array[0,1]=123

Thank You !

What are you calling the awk with? Many shells only support single diminsion arrarys, i.e. a list. If you have few enough items, then you might get away with using a formula to convert your 2D plan into a single value that you can repeat the function with the function with to recall the data later, but there are limits to the number of elements you can have, e.g. ksh will support up to 1024 (depending how you interpret the rules and how you use it)

Could you tell us a bit more about what you have tried, what the purspose is and data volumes. It might suggest something or steer someone with an idea.

Robin
Liverpool/Blackburn
UK

1 Like
[root@node3 ~]# cat infile 
A|123|446pr;
B|46|hello89
krp;
C|78|ystp90
67;
D|ga|456;
[root@node3 ~]# cat 2D_awk_array.sh 
awk -F\| ' 
    {
       gsub(/\n/,"",$0) 
       if (max_nf<NF) 
           max_nf=NF 
       max_nr=NR 
       for(x=1; x<=NF; ++x) 
           vector[NR-1,x-1]=$x 
    } 
END { 
       for(row=0; row<max_nr; ++row) { 
           for(col=0; col<max_nf; ++col) 
               if(length(vector[row,col]) != 0)
                  print "vector["row","col"]="vector[row,col]
       } 
    }' RS=";" ${1} 
[root@node3 ~]# bash 2D_awk_array.sh infile 
vector[0,0]=A
vector[0,1]=123
vector[0,2]=446pr
vector[1,0]=B
vector[1,1]=46
vector[1,2]=hello89krp
vector[2,0]=C
vector[2,1]=78
vector[2,2]=ystp9067
vector[3,0]=D
vector[3,1]=ga
vector[3,2]=456
1 Like

Hi Robin,

I tried below to do the same.

awk 'BEGIN{
FS="|";
RS=";";
}
{
print "We are here after begin";
--Here I want to assigne values in 2 D array
--say array[i,j]=$2
}
END {
;
}'File

Please help me here to improve the same.

Thanks,
[/FONT]

---------- Post updated at 09:45 AM ---------- Previous update was at 09:07 AM ----------

Thanks for the help.
Please help me understand your code.
1) I could not see where ";" line separator is defined.
2) Can I below line in starting of code:
awk 'BEGIN{
FS="|";
RS=";";
}
3) I am working on KSH , I suppose gsub might not work there.I see you are replacing "\n" with "". Will that be necessary even if I use this line RS=";";
It would be really helpful if you could guide me what are changes I should as KSH perspective.

Thanks,

}' RS=";" ${1} 

The font in red defines the Record Separator
I'm not familiar with ksh,sorry

1 Like

Shell is shell, awk is awk, they are independent and unrelated. Using ksh has nothing to do with whether you get gsub.

On some systems, especially Solaris, you need to use nawk to get gsub.

1 Like