Skip to content

Instantly share code, notes, and snippets.

@duruyao
Last active September 5, 2022 07:05
Show Gist options
  • Save duruyao/40c5dcd7aadd1507fd1d77d55b7e705d to your computer and use it in GitHub Desktop.
Save duruyao/40c5dcd7aadd1507fd1d77d55b7e705d to your computer and use it in GitHub Desktop.
Read two-dimensional array by Bash script.
#!/usr/bin/env bash
## date: 2021-12-14
## author: duruyao@gmail.com
## desc: read two-dimensional array by Bash script
data=(
'00007887.JPEG JPEG 500x333 500x333+0+0 8-bit sRGB 81.6KB 0.000u 0:00.000'
'00007893.JPEG JPEG 480x411 480x411+0+0 8-bit Grayscale Gray 256c 54KB 0.000u 0:00.000'
'00007894.JPEG JPEG 500x375 500x375+0+0 8-bit sRGB 52.9KB 0.000u 0:00.000'
'00007924.JPEG JPEG 500x375 500x375+0+0 8-bit Grayscale Gray 256c 53.5KB 0.000u 0:00.000'
'00007929.JPEG JPEG 694x948 694x948+0+0 8-bit sRGB 356KB 0.000u 0:00.000'
'00007944.JPEG JPEG 576x768 576x768+0+0 8-bit sRGB 95.4KB 0.000u 0:00.000'
)
rm -rf ./data.txt
## traverse a two-dimensional array
for line in "${data[@]}"; do
## write data to a file
echo "${line}" >> ./data.txt
its=( ${line} )
for it in "${its[@]}"; do
printf "%s!" "${it}"
done
printf "\n"
done
echo "-----------------------------------------------------------------------------------------"
## read a two-dimensional array from file (replace ` ` with `,`)
two_dim_array=( $(cat ./data.txt | grep -E "8-bit|sRGB" | sed "s/ /\,/g") )
## traverse a two-dimensional array
for line in "${two_dim_array[@]}"; do
## get a one-dimensional array (replace `,` with ` `)
one_dim_array=( $(echo ${line} | sed "s/\,/ /g") )
for it in "${one_dim_array[@]}"; do
printf "%s|" "${it}"
done
printf "\n"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment