Uppercase string in shell scripts

Possible duplicate:
A string of uppercase characters in shell scripts

I have a value like: james,adam,john I'm trying to make it james,adam,john (the first character of each name should be upper).

 echo 'james,adam,john' | sed 's/\<./\u&/g' 

does not work on all systems. In one system, its display is normal ... but not normal in another system ...

 A="james adam john" B=( $A ) echo "${B[@]^}" 

it throws some syntax error ... So, I do this through a long sing request during a loop that is too long. Is there a way to quickly access this?

+4
source share
4 answers

There are many ways to determine the "beginning of a name." This method selects any letter after the word boundary and converts it to uppercase. As a side effect, it will also work with names like Sue Ellen or Billy Bob.

 echo "james,adam,john" | perl -pe 's/(\b\pL)/\U$1/g' 
+7
source

With Perl:

 echo "james,adam,john" | \ perl -ne 'print join(",", map{ ucfirst } split(/,/))' 
+3
source

You can use awk like this to even out the first letter of each word in your input:

 echo "james,adam,john" | awk 'BEGIN { RS=","; FS=""; ORS=","; OFS=""; } { $1=toupper($1); print $0; }' 

OUTPUT

 James,Adam,John 
+2
source

Same method as TLP, but with GNU sed:

 echo "james,adam,john,sue ellen,billy-bob" | sed -r 's/\b(.)/\u\1/g' 

output:

 James,Adam,John,Sue Ellen,Billy-Bob 

If you want to iron out only the first letter, use instead:

 echo "james,adam,john,sue ellen,billy-bob" | sed 's/[^,]*/\u&/g' 

output:

 James,Adam,John,Sue Ellen,Billy-Bob 
+1
source

Source: https://habr.com/ru/post/1432546/


All Articles