Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

How do I insert a certain character string before and after each character in Bash, SED?

How do I insert a certain character string before and after each character in Bash, SED?

the character-string I want to insert before and after are somewhat similar to HTML tags.

like: [b]This would be BOLD test[/b]

[c=333333] This would be a GRAY colored font[/c]

[c=000000] This would be the alternative colored font[/c]

example being, the string is : "Hello 1 2 3"

should output: "[c=333333]H[/c][c=000000]e[/c][c=333333]l[/c][c=000000]l[/c][c=333333]o[c=333333] [/c] [c=333333]1[/c][c=000000]1[/c][c=333333]3[/c]

the result should be the alternating of those two colors via using those tags:[c=333333][/c] and [c=000000][/c]

1 Answer

Relevance
  • 1 decade ago
    Favorite Answer

    I understood the question in your headline, but your detailed question leaves me wondering what you're really asking.

    If the headline question is accurate, here's a simple example of how you would amend an input so that a particular string was added before each character, and a particular string was added after each character (the before and after string could be identical).

    echo abc | sed -e 's/\(.\)/A\1B/g'

    This results in "AaBAbBAcB".

    The "\(.\)" creates a "group" of the subexpression "." (any single character). The "\1" in the replacement part refers to "group 1". The replacement part puts "A" before the character, and "B" after the character.

Still have questions? Get your answers by asking now.