1
$ landscape=aws,azure
$ echo $landscape

expected o/p:

aws-landscape,azure-landscape

How I am doing it now..!?

$ landscape=aws,azure
$ GITLANDSCAPE=$(echo $Landscapes | sed 's/,/-landscape,/g' | sed 's/$/-landscape/g')
$ echo $GITLANDSCAPE

Is there a better way to do it?

5
  • 1
    Please do add what is your GOAL here? What should be expected output. Please post all details in your post and let us know then. Commented Jan 23, 2018 at 6:23
  • What's your actual question? Is your code not working? Commented Jan 23, 2018 at 6:23
  • Could you give us your input and corresponding expected output please? Commented Jan 23, 2018 at 6:30
  • I am getting the desired o/p..! i was just curious about the best way of doing it. Commented Jan 23, 2018 at 7:15
  • 1
    I'm voting to close this question as off-topic because it belongs to Code Review Stack Exchange Commented Jan 23, 2018 at 7:50

4 Answers 4

4

Replacing the comma is not a particularly good style.

You can do it like this:

landscape=aws,azure
echo $landscape | awk 'BEGIN{FS=",";OFS=",";suffix="-landscape"}{print $1suffix,$2suffix}'

Output:

aws-landscape,azure-landscape

In the BEGIN block you set the following variables:

  • FS: field separator
  • OFS: output field separator
  • suffix: you can use a variable here, in case you want to change the text later and don't want to change the text on several places in the code
Sign up to request clarification or add additional context in comments.

1 Comment

Really a nice one..! Thanks :)
2

Using a comma to delimit your strings is probably the main pain point here. The shell naturally supports space-separated tokens.

printf '%s-landscape\n' aws azure

If you want to do something a bit more complex, maybe a loop.

sep=''
for token in aws azure; do
    printf '%s%s-landscape' "$sep" "$token"
    sep=','
done

If you want to do something even more complex, perhaps put them in an array. (This is not Bourne/POSIX sh compatible, but a common extension in Ksh, Bash, etc.)

a=(aws azure)
for token in "${a[@]}"; do ...

As an aside, in Bash, there is also brace expansion:

printf '%s\n' {aws,azure}-landscape

This is tortured, but produces what you are asking:

printf '%s' {aws,\,azure}-landscape

The first comma separates the phrases between the braces. To include a literal comma in one of the phrases, we backslash it.

Comments

2

with sed, you can do as this:

echo $landscape | sed -E 's/,|$/-landscape&/g'
aws-landscape,azure-landscape

or, you can do with awk:

echo $landscape | awk -F, '{print $1"-landscape,"$2"-landscape"}'
aws-landscape,azure-landscape

6 Comments

@Guy, \0 means a fully match string.
I tried it out and found that, yes. It’s quite hard to search for more info tho! So unlike & which is just for specifically captured groups.. cheers.
I don't think that's portable to sed across the board, though. Is this a GNU sed extension?
@tripleee, do you mean the option -E?
No, I mean the \0sequence, though it's probably a feature of the also nonstandard -E extension on whichever version of sed this is.
|
0

If I understand you right, you have a variable holding a comma-separated list of words. You want to add a fixed string, -landscape, to each of these words, keeping the comma as separator.

This is one way to do it, provided that none of the words contains white space:

words1=ab,cd,ef,gh
words2=$(printf %s $words1 | xargs --delimiter , -L 1 printf "%s-landscape " | fmt -1 | paste -d, -s)
echo $words2

This would print

ab-landscape,cd-landscape,ef-landscape,gh-landscape

How it works: The xargs tears apart the words1 string into individual words and invokes printf to add -landscape to each. The resulting output is still separated by spaces, not by comma. With fmt, these words are put in separate lines, one line for each word. The final paste joins these lines with a comma.

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.