0

I would like a bash script to take the elements from array1 and output them differently in array2.

array1 -

array1=(s3://root/sub1/sub2/ 2022-10-22 2021-09-13 2020-08-15 s3://root/sub1/sub2/ 2022-09-22 2021-08-07 2020-02-03  s3://root/sub1/sub2/ 2022-08-22 2021-07-17 s3://root/sub1/sub2/ 2022-07-22)

array2-

array2=(s3://root/sub1/sub2/2022-10-22/ s3://root/sub1/sub2/2021-09-13/ s3://root/sub1/sub2/2020-08-15/ s3://root/sub1/sub2/2022-09-22/ s3://root/sub1/sub2/2021-08-07/ s3://root/sub1/sub2/2020-02-03/ s3://root/sub1/sub2/2022-08-22/ s3://root/sub1/sub2/2021-07-17/ s3://root/sub1/sub2/2022-07-22/)

So I basically want to take the url from array1 and append each date that follows it and store as a unique entry in array2.

My thought process is as follows - loop through array1 for each url entry write to new array and append the dates to that url that follow it from array1. I am unsure how to do this in bash however.

2
  • Loop through the array. If the value begins with s3:, save it to a variable. If not, append the current value to the variable, and add that to the result array. Commented Nov 14, 2022 at 8:22
  • Not sure whether I understand your question: Do you have problems looping over an array, or do you don't know how to append a new element to an array? Commented Nov 14, 2022 at 8:36

1 Answer 1

1

You're looking for something like this:

array2=()
for elem in "${array1[@]}"; do
  if [[ $elem = s3:* ]]; then
    pfix=$elem
  else
    array2+=("$pfix$elem")
  fi
done
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.