0

i need convert an array to list and wrtiting in a file, but the result is without space or delimiter, eg:

 declare -a INSTANCEID=`ec2-run-instances --instance-count 2 --key xxx --instance-type t2.micro [...] ${AMIID} | ./getInstanceId.pl -`

if i make an echo of INSTANCEID i have i-75bxxxfai-72bxxxfd. How can separate the two values (i-75bxxxfa and i-72bxxxfd) and writing in a file, one per lines?

I have tried with:

printf "%s\n"  "${INSTANCEID[@]}" > file.txt

but i have always the same output

8
  • The command launch two distinct instance, then i think is an array Commented Sep 21, 2016 at 9:39
  • @hellboy: I think you may want to re-check that, if they are distinct values, printf "%s\n" "${INSTANCEID[@]}" would have already printed them on separate lines. Commented Sep 21, 2016 at 9:41
  • echo 'i-75bxxxfai-72bxxxfd' |awk -v FS="-" -v OFS="\n" '{split($2,a,"i");print "$1" FS a[1], "$1" FS $3}' Commented Sep 21, 2016 at 9:51
  • Thanks but is no correct, every instance start with i- and can finish with i. I need to use also a for because the instance they can be more than 2 Commented Sep 21, 2016 at 9:58
  • Now, seems a string instead array, then i need separate the id's in a list, the id's starts with i- and can finish with i, can be more than two and can be long more characters Commented Sep 21, 2016 at 10:08

2 Answers 2

1

As per the comments over in the question description, OP wants to strip a string carrying instances.

A portable bash logic for this would be something like below. The below statements can be just run in command line.

$ INSTANCEID=i-75bxxxfai-72bxxxfd
$ IFS="-" read -ra instances <<<"$INSTANCEID"

$ for (( i=1; i<=$(( ${#instances[*]} -1 )); i++ )); \
    do printf "%s\n" "${instances[0]}-${instances[$i]//i} "; done

This will print the individual instances as

i-75bxxxfa
i-72bxxxfd

The idea here is

  • Split the input string from $INSTANCEID variable with de-limiter as - and read them into an array(read command with -a for array-read operation). The array contains entries as i, 75bxxxfai, 72bxxxfd
  • Now looping over the array instances over the total count "${#instances[*]}" - 1 and forming the string as "${instances[$0]}-${instances[$i]//i}" which is the string i followed by de-limiter - and the subsequent elements of the array with i removed ("${instances[$i]//i}")

You can see it working for any number of strings with multiple instance id's as:-

With 3 instances

$ INSTANCEID=i-75bxxxfai-72bxxxfdi-72bxxxfdi
$ IFS="-" read -ra instances <<<"$INSTANCEID"

$ for (( i=1; i<=$(( ${#instances[*]} -1 )); i++ )); \
    do printf "%s\n" "${instances[0]}-${instances[$i]//i} "; done

i-75bxxxfa
i-72bxxxfd
i-72bxxxfd

With 4 instances

$ INSTANCEID=i-75bxxxfai-72bxxxfdi-72bxxxfdii-7894xxuk
$ IFS="-" read -ra instances <<<"$INSTANCEID"

$ for (( i=1; i<=$(( ${#instances[*]} -1 )); i++ )); \
    do printf "%s\n" "${instances[0]}-${instances[$i]//i} "; done

i-75bxxxfa
i-72bxxxfd
i-72bxxxfd
i-7894xxuk

Read more about bash- Shell Parameter Expansion

Sign up to request clarification or add additional context in comments.

4 Comments

I have an error "Syntax error, expected token in line do printf "%s\n" "${instances[$0]}-${instances[$i]//i} "; done
@hellb0y77: Run it in a single line. for (( i=1; i<=("${#instances[*]}" - 1); i++ )); do printf "%s\n" "${instances[$0]}-${instances[$i]//i} "; done without any breaks, I added a line break for more readability
Working fine on my shell, GNU bash 4.1.2; Which version are you on ?
A more idiomatic loop: for inst in "${instances[@]:1}"; do printf '%s-%s\n' "${instances[0]}" "${inst//i}"; done.
0

set -- ${INSTANCEID[*]}

echo $@

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.