• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 364
  • Last Modified:

Help with Sed

I need ot modify a file with sed so that the first letter of every word is capital
I have it now so i use s/ a/ A/ etc... but I want to know if there is a shorter way thankz
1 Solution
First off
  s/ a/ A/
doesn't deal with 'a' at the beginning of a line, nor does it deal with multiple occurances of ' a' on the same line.  Since sed supports only Basic Regular Expressions, you'll need
  s/ a/ A/g

As for collapsing the a-z's, I don't that's going to be so easy to do.  The only way I can think of is non-sed procedural routine that walks the string and does the replacements.  Perl would probably be good for that.  Alternately, you could write a script that would generate the (rather long) sed.
sed 's/\<\(.\)/ x\
/g' | sed -e :a -e '/ x$/{s/ x$//' -e h -e s/.//g -e N -e y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/ -e H -e s/.//g -e N -e H -e g -e 's/\n//g' -e ta -e '}'

(as  chris_calabres suggested, perl would be more suited to this than sed)
you can use more efficiently the tr command (traduct)

here a part og the manual:
      tr [-Acs] string1 string2


      tr copies the standard input to the standard output with substitution
      or deletion of selected characters.  Input characters from string1 are
      replaced with the corresponding characters in string2. If necessary,
      string1 and string2 can be quoted to avoid pattern matching by the

                        In a case conversion, however, the string2 array
                          contains only those characters defined as the
                          second characters in each of the toupper or
                          tolower character pairs, as appropriate. For

                          tr  '[:upper:]' '[:lower:]'

for example:
root@med1091[/]echo AAA | tr '[:upper:]' '[:lower:]^J'

tr is useless for this purpose 'cause it translates all characters (read man pages). Also keep in mind that classes are in GNU's tr, which might not be used on all UNIXs.

See ozo's comment.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now