These notes are supplemental to some basic links on the parent page (BusserTechNotes#RegEx).

convert case in sed substitution

In the first case you are looking for the characters a-z and turn them to upper case, your search does not include a space, hence it stops at the first space. The correct regex is

echo dennis decker jensen | sed -re 's/[a-z ]+/\U&/'

Note the space after the z.

In the second case if you only want to convert the first D use

echo dennis decker jensen | sed -re 's/[a-z]/\U&/' 
Dennis decker jensen
I don't think there is anything wrong with sed, just how you use the regex.

Also note that regex's sometimes are different between different programs/languages. Perl regex's can do more than sed regex's for example.

convert single i occurrences to upper case

sed -e 's/i/I/g' file

convert all occurrences of upper case I (when these follow a letter) to lower case:

sed -e 's/\([a-zA-Z].*\)I\([a-zA-Z].*\)/\1i\2/g' file


echo 'iJKLMNOiIiPQRS' | sed -e 's/\([a-zA-Z].*\)I\([a-zA-Z].*\)/\1i\2/g'

specifying regex word delimiters

specify regex "word delimiters":

/\bWORD\b/                          # word is framed by blanks
/\<WORD\>/                          # match whole word inside < >
/[[:blank:]]WORD[[:blank:]]/        # word is framed by blanks (alternate)

Your choices will vary with your Unix distro and sed version.
Topic revision: 23 Sep 2011, JamesBusser
This site is powered by the TWiki collaboration platformCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Foswiki? Send feedback
Powered by Olark