-   Linux - Newbie (
-   -   sed - change and capitalize some letters in words (

anascu 05-09-2011 02:52 PM

sed - change and capitalize some letters in words

I'm trying to use Sed to change all "l" letters with "I" in capitalized words something like this:


- and as opposite change the all "I" with "l" in a word in small letters such as:

2. DiIbert -> Dilbert

For the first case I'm using the below sentence which unfortunately changes words such as "let" in "It"


sed 's/[:upper:]*l[:upper:]*/I/g'
For the second case the below command seems to work but it might have a side effect as the one used in the first case:


sed 's/[:lower:]*I[:lower:]*/l/g'
Could anyone show me a better option?


T3RM1NVT0R 05-09-2011 03:08 PM

@ Reply
Hi Adrian,

Try this sed 's/l/L/g' source_filename > new_filename

anascu 05-09-2011 03:54 PM

Thanks Terminator but this wouldn't work as in the same file I have words in small letters and capital letters that don't need to be changed. There are only the cases where "l" is there instead of "I" in capitalized words and "I" instead of "l" in low letters words. Here's an example:



T3RM1NVT0R 05-09-2011 05:09 PM

@ Reply
Hi Adrian,

Well whatever commands that I have seen with sed related to changing a string or a letter in a file it require both source and destination file. Have a look at this link: and you will understand how sed works and you will also find the way that I gave for changing a lower case letter to a uppercase.

In the example you gave me if you want lowercase i to be changed as uppercase I then you can go with the following command:

sed 's/i/I/g' source_filename > destination_filename

If you want to have the changes back in the original file then you can run the above command as below:

sed 's/i/I/g' source_filename > destination_filename && cat destination_filename > source_filename

Note: Make a backup of source file so that you do not loose whatever you saved if something goes wrong.

grail 05-09-2011 09:28 PM

I think the issue you are having is you nave no real way of knowing how many letters before or after and hence the appropriate metacharacter is hard to find.

Let me explain:

You are using * which says zero or more of the preceding which allows 'let' to be changed as there are zero capitals before and after 'l'.
You can't use + because now you would always need 'l' to be after the first character and prior to the last, which obviously cannot be guaranteed

So maybe if we are a little clever we can do something like:

sed -r 's/(\b|[[:upper:]]+)l(\b|[[:upper:]]+)/\1I\2/g' file

anascu 05-10-2011 03:10 AM

Thanks Grail,

This works excellent .. in Linux. Thanks again. Now I need to find a way to replicate it to OS X.


grail 05-10-2011 06:32 AM

As OS X is just Darwin Linux with a fancy wrapper, what does not work? Or is the version of sed very different?

anascu 05-10-2011 12:50 PM

Actually it was easy. I had to use the -E switch instead of -r:


sed -E 's/(\b|[[:upper:]]+)l(\b|[[:upper:]]+)/\1I\2/g' file
That's it.

Thanks again

All times are GMT -5. The time now is 08:42 AM.