LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-09-2011, 02:52 PM   #1
anascu
LQ Newbie
 
Registered: May 2011
Posts: 4

Rep: Reputation: 0
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.srt
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:

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

Thanks,
Adrian
 
Old 05-09-2011, 03:08 PM   #2
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
@ Reply

Hi Adrian,

Try this sed 's/l/L/g' source_filename > new_filename
 
Old 05-09-2011, 03:54 PM   #3
anascu
LQ Newbie
 
Registered: May 2011
Posts: 4

Original Poster
Rep: Reputation: 0
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.
Adrian
 
Old 05-09-2011, 05:09 PM   #4
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, SLES, CentOS, Red Hat
Posts: 2,385

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
@ 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.
 
Old 05-09-2011, 09:28 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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
 
1 members found this post helpful.
Old 05-10-2011, 03:10 AM   #6
anascu
LQ Newbie
 
Registered: May 2011
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks Grail,

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

Adrian
 
Old 05-10-2011, 06:32 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
As OS X is just Darwin Linux with a fancy wrapper, what does not work? Or is the version of sed very different?
 
Old 05-10-2011, 12:50 PM   #8
anascu
LQ Newbie
 
Registered: May 2011
Posts: 4

Original Poster
Rep: Reputation: 0
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' file
That's it.

Thanks again

Last edited by anascu; 05-10-2011 at 12:52 PM. Reason: Highlight the CODE section
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete text between key words : sed shalomajay Programming 7 05-11-2011 06:22 PM
[SOLVED] Awk - finding and counting words specific letters within mora978 Programming 9 10-13-2010 10:45 AM
how to remove words in a file using sed or any other command Kilam orez Linux - Newbie 4 11-30-2009 08:52 AM
Find several different words and replace with one using sed. Techno Guy Linux - Newbie 18 07-06-2009 07:16 AM
sed or awk question - replace caps with small letters computera Linux - General 1 12-30-2003 04:39 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration