1

I am totally new on shell scripting (actually three days only :) ) so don't shoot me .. !!

In my script I have a code that looks like that:

string="this-is-my-string"

and I like to modify the content of the variable, in order to become like that:

string="This Is My String"

I don't look for the regex that modify the original string into the modified version. I already have find the solution here.

What I am looking for, is how to modify the text inside the variable using the sed command. To be honest, I don't really know if that possible with sed. In case that modification it is not possible to be done with sed is there any other method to achieve the same result ?

Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
KodeFor.Me
  • 13,069
  • 27
  • 98
  • 166

6 Answers6

6
string=$(sed 's/^./\u&/; s/-\(.\)/ \u\1/g' <<< $string)

Example:

sdlcb@Goofy-Gen:~/AMD$ cat File
#!/bin/bash

string="this-is-my-string"
string=$(sed 's/^./\u&/; s/-\(.\)/ \u\1/g' <<< $string)    
echo "$string"

AMD$ ./File
This Is My String

s/^./\u&/ => Capitalize the first character \u for uppercase. & => the matched pattern.

s/-\(.\)/ \u\1/g => substitute - followed by character to space followed by uppercase of the character. ( ) used to group pattern and \1 => first group, in this case only 1 such group is present.

Arjun Mathew Dan
  • 5,240
  • 1
  • 16
  • 27
4

Yeah that's possible, try something like:

#!/bin/bash
string="this-is-my-string"
string=$(echo ${string} | sed 's/\-/ /g')
echo ${string}
Jasper
  • 444
  • 3
  • 19
  • I haven't downvoted you but i think the person who did may have done so because OP also changes the first letters to capitals. Just a heads up :) –  Dec 30 '14 at 10:43
  • Yeah didn't pay any attention to that as the OP stated he already solved that problem and said he was just after a solution to use sed for manipulating variables. – Jasper Dec 30 '14 at 10:44
  • Using echo has gotchas - e.g. quotes will disappear. – Ondra Žižka May 02 '18 at 23:56
4

A pure Bash (Bash≥4) solution with parameter expansions:

$ string='this-is-my-string'
$ IFS=- read -r -a ary <<< "$string"
$ string="${ary[@]^}"
$ echo "$string"
This Is My String
gniourf_gniourf
  • 44,650
  • 9
  • 93
  • 104
  • Which part capitalises the beginning of the words ? –  Dec 30 '14 at 11:38
  • @Jidder: it's the single caret (`^`) that appears in `${ary[@]^}`: this will expand all the fields of the array `ary` with the first character converted to uppercase. – gniourf_gniourf Dec 30 '14 at 11:40
  • Ahh right, did not know you could do that,thanks! Are there any other transformations that can be done in a similar way ? –  Dec 30 '14 at 11:41
  • 1
    @Jidder: Apart from upper/lower-casing first character or whole string, replacements, substrings (prefix/suffix), there are no other transformations. – gniourf_gniourf Dec 30 '14 at 20:38
3

If you are interested this is quite simple in Python, from the interpreter:

>>> string = "this-is-my-string"
>>> string = ' '.join([ s.capitalize() for s in  string.split('-')])                                                                                                          
>>> print string
This Is My String
Juan Diego Godoy Robles
  • 14,447
  • 2
  • 38
  • 52
1

You can try it like this too

 echo '"this-is-my-string"'|sed 's/-/ /g;s/\([^"]\)\(\S*\s*\)/\u\1\2/g'

or

echo "$string"|sed 's/-/ /g;s/\([^"]\)\(\S*\s*\)/\u\1\2/g'

the \S is a metacharacter that matches any character that is not a space/whitespace and \s matches any characters that is a space

repzero
  • 8,254
  • 2
  • 18
  • 40
1

Here is an awk solution:

echo "this-is-my-string" | awk -F- '{for (i=1;i<=NF;++i) $i=toupper(substr($i,1,1)) substr($i,2)}1'
This Is My String

string='this-is-my-string'
string=$(awk -F- '{for (i=1;i<=NF;++i) $i=toupper(substr($i,1,1)) substr($i,2)}1' <<< "$string")
echo "$string"
This Is My String

Here is another awk

echo "this-is-my-string" | awk -F- '{for(i=1;i<=NF;i++)sub(/./,toupper(substr($i,1,1)),$i)}1'
This Is My String
Jotne
  • 40,548
  • 12
  • 51
  • 55