TECH SOLUTIONS

Click here to edit subtitle

Forums

Post Reply
Forum Home > Unix Learnings > Shell Script: Capitalize first letter of every word of a line

Sourav Gulati
Site Owner
Posts: 83

Lets take an example file as follows:


$ cat file

capiltalize first letter of every word of a line

using shell script


If you want to capitalize first letter of every word of all lines, it can be done as follows:


$ sed -i -e 's/^./\U&/g; s/ ./\U&/g' file

$ cat file

Capiltalize First Letter Of Every Word Of A Line

Using Shell Script


^. - represents  character that follows beginning of a line i.e. first character of the line

  .(space and dot) - represents every character that follows space

U - represents uppercase ( L can be used for lowercase if needed)

If you want line to perform this operation on selective lines , you can provide line number as follows:


$ sed -i -e '1,3 s/^./\U&/g; s/ ./\U&/g' file

Now, this operation will be performed on first three lines

--


January 30, 2013 at 6:20 AM Flag Quote & Reply

Anonymous
Member
Posts: 3

Thanks Sourav.


New Case Below:

Source="ONE TWO THREE FOUR"

Target="One Two Three Four"


Solution is below:

Target=`echo $Source | tr [A-Z] [a-z] | sed -e 's/^./\U&/g; s/ ./\U&/g'`

echo $Target

One Two Three Four



- Parvinder

Site Owner

parvindersingh.webs.com

www.facebook.com/iamparv

February 14, 2013 at 4:14 AM Flagged Quote & Reply

Anonymous
Member
Posts: 3

To capitalize all the letters of 1st line

sed -i -e '1 s/.*/\U&/g;' file

--


November 27, 2013 at 3:12 AM Flagged Quote & Reply

You must login to post.