Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 12-01-2013, 12:59 PM   #1
Registered: Dec 2012
Distribution: CentOS
Posts: 38

Rep: Reputation: Disabled
Use sed to replace a single letter?

I cant seem to get sed to replace a single letter, when i use
sed -e 's/J/M/g'
It ends up matching more than a single letter of the data and replaces it to.

The data looks like

it will look like

Not sure how to confine sed to a single letter match?
Old 12-01-2013, 01:18 PM   #2
LQ Veteran
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390
The g makes it global (for all instances in the line). If you remove the g then only the first instance will be changed:
sed 's/J/M/'
You can change the second occurrence like this:
sed 's/J/M/2'
If the letter is the last on a line:
sed 's/J$/M/'
- Sed - An Introduction and Tutorial
- sed, a stream editor
Old 12-01-2013, 01:46 PM   #3
Senior Member
Registered: Feb 2003
Distribution: debian
Posts: 3,299
Blog Entries: 1

Rep: Reputation: 685Reputation: 685Reputation: 685Reputation: 685Reputation: 685Reputation: 685
^ as above. The /g makes it do it multiple times for every instance in a line. Drop the g and it does the first instance only.
Old 12-02-2013, 12:45 AM   #4
Registered: Mar 2013
Distribution: Redhat, Ubuntu
Posts: 525

Rep: Reputation: 141Reputation: 141
Originally Posted by Spazztic_Killer View Post
Not sure how to confine sed to a single letter match?
You can use word boundary (\< \> ot \b) to achieve it.

sed 's/\<J\>/M/g'
sed 's/\bJ\b/M/g'

Last edited by mddnix; 12-02-2013 at 12:48 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Sed replace Help replace for (<a> guessity Linux - Newbie 1 11-18-2011 10:05 AM
[SOLVED] Using File as Replace in Sed abercrombieande Programming 9 03-04-2011 03:55 PM
Replace part of string-delete last letter of it cgcamal Programming 15 06-18-2009 12:08 PM
How do I replace ' with sed.... @ngelot Linux - Newbie 2 11-02-2007 08:04 PM
sed help, replacing a letter with a word GridX Linux - Newbie 1 09-24-2003 11:21 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:52 PM.

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