Suppose I have an array where each element in the array is in the following format:
someText (x1,y1,z1) (x2,y2,z2) (x3,y2,z3) KEY
What is the a
This is how you can get first and last element of your string in the array:
echo "someText (x1,y1,z1) (x2,y2,z2) (x3,y2,z3) KEY" | rev | cut -d " " -f 1 | rev
echo "someText (x1,y1,z1) (x2,y2,z2) (x3,y2,z3) KEY" | cut -d " " -f 1
You can do something like this (where OLDARRAY
is the previous array and NEWARRAY
is the new one).
NEWARRAY=()
for I in "${!OLDARRAY[@]}"; do
IFS=$' ' read -ra T <<< "${OLDARRAY[I]}"
NEWARRAY[I]="${T[0]} ${T[@]:(-1)}"
done
Example output:
> set | grep ^NEWARRAY
NEWARRAY=([0]="someText KEY" [1]="someText2 KEY2")
echo "someText (x1,y1,z1) (x2,y2,z2) (x3,y2,z3) KEY"|awk '{print $1,$NF}'