Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Help with Sed

Posted on 1999-06-30
4
Medium Priority
?
340 Views
Last Modified: 2010-04-21
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
0
Comment
Question by:shatter546
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 14

Expert Comment

by:chris_calabrese
ID: 2011290
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/
  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.
0
 
LVL 84

Expert Comment

by:ozo
ID: 2011291
sed 's/\<\(.\)/ x\
\1\
/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)
0
 
LVL 2

Accepted Solution

by:
bedot earned 200 total points
ID: 2011292
you can use more efficiently the tr command (traduct)

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

...

 DESCRIPTION
      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
      shell.


    ...
                        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
                          example:

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




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

0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 2011293
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.
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question