Reputation: 57
I have the following array:
declare -a case=("060610" "080813" "101016" "121219" "141422")
I want to generate another array where the elements have whitespaces inserted appropriately as:
"06 06 10" "08 08 13" "10 10 16" "12 12 19" "14 14 22"
I got till handling the elements individually using sed as:
echo '060610' | sed 's/../& /g'
But I am not being able to do it while using the array. The sed confuses the white spaces in between the elements and I am left with the output:
echo "${case[@]}" | sed 's/../& /g'
Gives me:
06 06 10 0 80 81 3 10 10 16 1 21 21 9 14 14 22
Can someone help?
Upvotes: 1
Views: 244
Reputation: 123470
You can use printf '%s\n' "${case[@]}" | sed 's/../& /g'
to get each number on a separate line and avoiding the space problem:
$ declare -a case=("060610" "080813" "101016" "121219" "141422")
$ printf '%s\n' "${case[@]}" | sed 's/../& /g'
06 06 10
08 08 13
10 10 16
12 12 19
14 14 22
If you want it back into an array, you can use mapfile
Upvotes: 2
Reputation: 780974
You need to loop over the array, not echo it as a whole, because you don't get the grouping when you echo it.
declare -a newcase
for c in "${case[@]}"
do
newcase+=("$(echo "$c" | sed 's/../& /g')")
done
Upvotes: 2