| Forum Home > Unix Learnings > Shell Script: Capitalize first letter of every word of a line | ||
|---|---|---|
|
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 | |
--
| ||
|
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 | |
| ||
|
Member Posts: 3 |
To capitalize all the letters of 1st line sed -i -e '1 s/.*/\U&/g;' file | |
--
| ||