KodeFor.Me
KodeFor.Me

Reputation: 13511

Capitalize words in a bash variable using sed

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 ?

Upvotes: 1

Views: 10319

Answers (6)

Jotne
Jotne

Reputation: 41456

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

Upvotes: 1

Juan Diego Godoy Robles
Juan Diego Godoy Robles

Reputation: 14955

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

Upvotes: 3

gniourf_gniourf
gniourf_gniourf

Reputation: 46833

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

Upvotes: 4

repzero
repzero

Reputation: 8412

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

Upvotes: 1

Arjun Mathew Dan
Arjun Mathew Dan

Reputation: 5298

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.

Upvotes: 6

Jasper
Jasper

Reputation: 484

Yeah that's possible, try something like:

#!/bin/bash
string="this-is-my-string"
string=$(echo ${string} | sed 's/\-/ /g')
echo ${string}

Upvotes: 4

Related Questions