LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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
 
LinkBack Search this Thread
Old 11-04-2010, 10:08 AM   #1
ieatbunnies
Member
 
Registered: Feb 2010
Posts: 37

Rep: Reputation: 14
sed replacing a specific character with a specific number


i want to change all the "B's" in a text file to be replaced witha specific number such as"1" but in the same text i would like to replace all the "C's" with "2" and "D's" with 3

Code:
sed 's/b/1/g' $fl.old > $fl
sed 's/c/2/g' $fl.old > $fl
sed 's/d/3/g' $fl.old > $fl
but i know that is not right.
 
Old 11-04-2010, 10:11 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

Untested, but this should work:

sed -i.bak -e 's/b/1/g' -e 's/c/2/g' -e 's/d/3/g' infile

With the -e option you can string together multiple sed statements.
The -i.bak makes in file changes and saves the original with an added .bak.

Hope this helps.
 
Old 11-04-2010, 10:14 AM   #3
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,410
Blog Entries: 1

Rep: Reputation: 951Reputation: 951Reputation: 951Reputation: 951Reputation: 951Reputation: 951Reputation: 951Reputation: 951
1. You can combine the three commands:

Code:
sed -e 's/b/1/g' -e 's/c/2/g' -e 's/d/3/g' fl.old > fl
Why $? Do you want to pass the file names from variables?
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Sed - Replacing only text with several specific lines excluded Potatos Linux - Newbie 6 06-17-2010 11:51 PM
[SOLVED] SED and Replacing Specific occurrence or Range of Lines bridrod Linux - Newbie 7 08-27-2009 09:59 AM
[SOLVED] SED and Replacing Specific Line bridrod Linux - Newbie 6 08-24-2009 12:28 PM
SED ? get number before specific word czezz Programming 3 08-11-2009 06:00 PM
Replacing text on specific lines with sed or awk? Lantzvillian Linux - Newbie 5 10-17-2007 09:00 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration