Variable Transformations
#
tab delimited ids into multiple parametersExample input in this case is a function that returns the default subnets for a region.
One thing to pay attention to here, when you echo without quoting the variable here, you'll end up with a space delimited string, but when you echo a quoted variable, you end up with the original, which is a TAB (\t
) delimited string. This can affect further flow of a script.
With people I've worked with there were generally two camps.
- People that try to quote as little as possible, because the scripts pretty much always work
- Another set of people, including myself, that don't like auto-magic things all that much, and use quotes everywhere for more guaranteed consistent outcome.
Honestly, in most simple scripts, I've not seen that this debate matters much, but I find bash scripts fairly difficult to debug, and so I try to avoid the surprises. The quoting also adheres to the shellcheck
standard, which will return an error like this.
Now, all that's left is to turn the string into multiple parameters.
Note: This will only work with GNU sed, not the POSIX sed
To break down some parts of the sed
expression:
/^$/!
states to match only lines not matching this patterns which in this case makes it ignore empty lines.s/[^\t]*\t*/--subnet-id &/g
 is a substitution, matching any non TAB character until it reaches a tab, then replaces everything it matched up until now with--subnet-id &
, where the&
 means the entire match thus far. Sosubnet-d6938df9\t
 turns into--subnet-id subnet-d6938df9\t
. Theg
 option makes it then continue until there are no more matches.