Thursday, 25 April 2013

Unix Shell Script: Capitalize first letter of every word of a line

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

No comments:

Post a Comment