![]() |
sed - change and capitalize some letters in words
Hello,
I'm trying to use Sed to change all "l" letters with "I" in capitalized words something like this: 1. ONlON -> ONION - 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" Code:
sed 's/[:upper:]*l[:upper:]*/I/g' test.srtCode:
sed 's/[:lower:]*I[:lower:]*/l/g' test.srtThanks, Adrian |
@ Reply
Hi Adrian,
Try this sed 's/l/L/g' source_filename > new_filename |
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:
Code:
LINUXQUESTlONS.ORG IS LOOKlNG FOR PEOPLE INTERESTED IN WRITING EDITORIALS. |
@ 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: http://www.grymoire.com/Unix/Sed.html#toc-uh-17 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. |
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: Code:
sed -r 's/(\b|[[:upper:]]+)l(\b|[[:upper:]]+)/\1I\2/g' file |
Thanks Grail,
This works excellent .. in Linux. Thanks again. Now I need to find a way to replicate it to OS X. Adrian |
As OS X is just Darwin Linux with a fancy wrapper, what does not work? Or is the version of sed very different?
|
Actually it was easy. I had to use the -E switch instead of -r:
Code:
sed -E 's/(\b|[[:upper:]]+)l(\b|[[:upper:]]+)/\1I\2/g' fileThanks again |
| All times are GMT -5. The time now is 11:59 PM. |