LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 12-01-2013, 12:59 PM   #1
Spazztic_Killer
Member
 
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
Code:
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

lastname:firstname:middlename
Quote:
Smith:James:J
it will look like

Quote:
Smith:Mames:M
Not sure how to confine sed to a single letter match?
 
Old 12-01-2013, 01:18 PM   #2
druuna
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:
Code:
sed 's/J/M/'
You can change the second occurrence like this:
Code:
sed 's/J/M/2'
If the letter is the last on a line:
Code:
sed 's/J$/M/'
- Sed - An Introduction and Tutorial
- sed, a stream editor
 
Old 12-01-2013, 01:46 PM   #3
Shadow_7
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
mddnix
Member
 
Registered: Mar 2013
Distribution: Redhat, Ubuntu
Posts: 525

Rep: Reputation: 141Reputation: 141
Quote:
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.

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

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


Reply


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

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

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

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration