I'm trying to create an array in BASH with the following command:
# Normally a loop, but for this example, n=1
arr=( `awk 'NR == n' n=1 "$file" | tr ',' " "` );
echo ${arr[@]};
echo ${arr[2]};
$file contains:
"1","test_id = \"5\"","test_id <> \"5\""
"2","test_id = \"6\"","test_id <> \"6\""
Output:
"1" "test_id = \"5\"" "test_id <> \"5\""
"test_id
I'm expecting output of ${arr[2]} to be test_id <> "5" but I actually get "test_id.
However, when I manually input the data like so, I get the correct result:
arr=( "1" "test_id = \"5\"" "test_id <> \"5\"" );
echo ${arr[@]};
echo ${arr[2]};
Output:
1 test_id = "5" test_id <> "5"
test_id <> "5"
Any help would be appreciated,
Thanks.
""and not\". That's going to give some headaches too. What happens with commas and backslashes inside the field? I don't think your variant is too hard to handle, but because it doesn't match the default, you will need to tweak tools such as Python's CSV module to handle it properly. Just a heads-up.