(Solved) How to capitalize all first characters of text?

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
ITSMERSH

(Solved) How to capitalize all first characters of text?

#1 Post by ITSMERSH »

Hi.

In May 2015 I was in the need to have a function to capitalize first letter of file name.

Now I have the need to capitalize all first characters of all words in a text file or at least for per one line of text.

How to achieve?

Thanks
Last edited by ITSMERSH on Mon 17 Dec 2018, 19:51, edited 2 times in total.

User avatar
fredx181
Posts: 4448
Joined: Wed 11 Dec 2013, 12:37
Location: holland

#2 Post by fredx181 »

I think here you find :
https://stackoverflow.com/questions/153 ... -using-sed

The sed examples capitalize first char even if there's a - or a . between the words.
The awk example does not (may be more what you're looking for)

Code: Select all

# echo "my word" | sed -e "s/\b\(.\)/\u\1/g"
My Word
# echo "my-word" | sed -e "s/\b\(.\)/\u\1/g"
My-Word
# echo "my word" | awk '{for(i=1;i<=NF;i++){ $i=toupper(substr($i,1,1)) substr($i,2) }}1'
My Word
# echo "my-word" | awk '{for(i=1;i<=NF;i++){ $i=toupper(substr($i,1,1)) substr($i,2) }}1'
My-word
Fred

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#3 Post by musher0 »

Hello Rainer.

Here are a couple of ways of doing it in bash:
1) https://www.linuxquestions.org/question ... ing-268182

2)

Code: Select all

read -ra words <<< "<sentence>" && echo "${words[@]^}"
Source

A search on ask.com (or any serious search engine) will give you even
more solutions, e.g.:
https://www.ask.com/web?qsrc=1&o=0&l=di ... archTopBox

IHTH.
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#4 Post by MochiMoppel »

Many ways to do this. Basically something similar to the sed example I proposed in 20015 should do it.

If file.txt contains
  • these are my-words
    in 2 lines of text
then

Code: Select all

sed -r "s/^.|[ 	]./\U&/g" file.txt
should produce
  • These Are My-words
    In 2 Lines Of Text
Note that the [] brackets contain a space and a tab.
The sed expression turns the first character at line start (^.) or (|) any character (.) following a space or a tab ([ ]) into uppercase.
If for example you also need to change the capitalization of words that immediately follow a colon, you could add a colon to the characters in the [] brackets.

ITSMERSH

#5 Post by ITSMERSH »

@all

That's a lot more solutions than I'd expected.

And all of them much shorter than my own hack.

Cool, thanks! 8) :)

zagreb999
Posts: 567
Joined: Fri 11 Apr 2014, 06:39
Location: Yugoslavija

wps office writer

#6 Post by zagreb999 »

wps office writer can do that!

jafadmin
Posts: 1249
Joined: Thu 19 Mar 2009, 15:10

#7 Post by jafadmin »

MochiMoppel wrote:Many ways to do this. Basically something similar to the sed example I proposed in 20015 should do it. ...
Time traveler detected! So cool, man. So very cool .. 8)

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#8 Post by MochiMoppel »

:?:

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#9 Post by musher0 »

MochiMoppel wrote::?:
I think that jafadmin is saying -- in poetic :) terms -- that there has been
a solution to this problem for a long time? ;)
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
fabrice_035
Posts: 765
Joined: Mon 28 Apr 2014, 17:54
Location: Bretagne / France

#10 Post by fabrice_035 »

Image
Bionicpup64-8.0 _ Kernel 5.4.27-64oz _ Asus Rog GL752

ITSMERSH

#11 Post by ITSMERSH »

:lol:

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#12 Post by musher0 »

MochiMoppei is way ahead of us, everybody should know that! :lol:
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#13 Post by MochiMoppel »

removed

User avatar
puppy_apprentice
Posts: 299
Joined: Tue 07 Feb 2012, 20:32

#14 Post by puppy_apprentice »

musher0 wrote:MochiMoppei is way ahead of us, everybody should know that! :lol:
You should wrote in more like haiku (Mochi is from Japan) style:

Code: Select all

MochiMoppei
everybody should know that!
is way ahead of us
More haiku:

http://www.haiku.art.pl/

Code: Select all

Aki mohaya
sono higurashi no
inochi ka na

Post Reply