10.3.32 csvToArray (stores the two-dimensional array of CSV data)
Syntax
csvToArray array-name [file-path-name]
Description
This command stores data of the CSV file specified for argument in the two-dimensional array. Specifically, this command stores data delimited by comma as the value of each element of two-dimensional array. Example of support of elements of CSV data and two-dimensional array is as follows:
CSV data(data.csv) name,value,id Yokohama,200,1 Kawasaki,100,2 support of elements of two-dimensional array(array) and CSV data (data.csv) array[0][0]=name array[0][1]=value array[0][2]=id array[1][0]=Yokohama array[1][1]=200 array[1][2]=1 array[2][0]=Kawasaki array[2][1]=100 array[2][2]=2
The double quotation marks that enclose the field of the CSV data are deleted and the file is stored. The double quotation marks described consecutively in the field are interpreted as one double quotation mark and the file is stored.
Example:
CSV data |
Data stored in the two-dimensional array |
---|---|
"abc" |
abc |
"a""b""c" |
a"b"c |
Arguments
- array-name
-
This command specifies the name of array in which data is stored.
- file-path-name
-
This command specifies the path of the CSV file. If you do not specify the file path name, input from the standard input.
The size of CSV data that can be input is 100KB or less. If you input data larger than 100KB, the execution time of the job may become longer.
Return codes
Return code |
Meaning |
---|---|
0 |
Normal termination |
Not less than 1 |
Termination with an error |
Notes
-
Do not specify an array name starting with "adsh" for the array-name argument. If you specify such an array name, it might conflict with the name of another variable used within the part and produce invalid output.
-
The character string that can be specified for array-name is the same as character string that can be used as variable name. However, whether the character string can be specified is not verified.
Examples
CSV data(data.csv)
name,value,id
Yokohama,200,1
Kawasaki,100,2
# Stores data of the CSV file in the two-dimensional array.
csvToArray array data.csv
echo "${array[1][0]}" # "Yokohama" is output.
echo "${array[1][1]}" # "200" is output.
# Stores the data that the first line is deleted from the CSV file in two-dimensional array.
"${ADSH_DIR_CMD}awk" '{if(FNR!=1){print $0}}' data.csv | csvToArray array
echo "${array[0][0]}" # "Yokohama" is output.
echo "${array[0][1]}" # "200" is output.