I need to assign a variable to each field, to later generate HTML. My data file looks like this:
field1;field2;field3 # << RECORD 1
this;is;second;record # << RECORD 2
Is there a way to generate an array for each record, based on the record number? Like arrayNR, so:
array1[0] = "field1"; array1[1] = "field2";
^ record number
array2[0] = "this"; array2[1] = "is"; array2[2] = "second"; array2[3] = "record"
^ record number
Using Gnu awk multidimensional arrays and split
, for example:
$ awk '{array[NR][1]; split($0,array[NR],";"); for(i in array[NR]) print array[NR][i]}' file
{
array[NR][1] # initiate array
split($0,array[NR],";") # split to array
for(i in array[NR]) # iterate thru all array elements
print array[NR][i] # output array value
}
With GNU awk you can have multidimensional arrays:
gawk -F\; '{for(i=1;i<=NF;i++)a[NR][i]=$i}END {for(i in a) for (j in a[i]) print a[i][j]}' file
With other awks you can use:
awk -F\; '{for(i=1;i<=NF;i++)a[NR,i]=$i}END {for(i in a) {split(i,b,SUBSEP); print a[b[1],b[2]]}}' file
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.