LinuxQuestions.org
Help answer threads with 0 replies.
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 01-20-2006, 09:57 AM   #1
Smokey
Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 313

Rep: Reputation: 30
replacing word for word


I have a text file and I would like to replace a name for another, the name is Daniel and I would like to replace all instances of Daniel for Danny. how can I do this automatically with a command?
 
Old 01-20-2006, 10:24 AM   #2
tuxrules
Senior Member
 
Registered: Jun 2004
Location: Chicago
Distribution: Slackware64 -current
Posts: 1,152

Rep: Reputation: 60
Use Sed,

Code:
sed `s/Daniel/Danny/` filename.txt
Remember that sed does not alter the original file, it just displays the modified output. If you want to put the output in a new file, use bash output redirection.

Code:
sed `s/Daniel/Danny/` filename > newfile
Tux,

Last edited by tuxrules; 01-20-2006 at 10:29 AM.
 
Old 01-20-2006, 10:27 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
Hi,

There are more then one way to do this, here's one using sed:

sed -i.bak 's/Daniel/Danny/g' <textfile>

The s (sed -i.bak 's/Daniel/Danny/g' <textfile>) tells sed that you want to replace, the g on the end makes it global, not just the first one on a line.
sed looks for Daniel (sed -i.bak 's/Daniel/Danny/g' <textfile>),
and put Danny (sed -i.bak 's/Daniel/Danny/g' <textfile>) in its place.

The -i.bak option makes a copy of the original named <textfile>.bak, changes are made in <textfile>

Hope this helps.

Last edited by druuna; 01-20-2006 at 10:28 AM.
 
Old 01-20-2006, 10:40 AM   #4
tuxrules
Senior Member
 
Registered: Jun 2004
Location: Chicago
Distribution: Slackware64 -current
Posts: 1,152

Rep: Reputation: 60
Quote:
Hi,

There are more then one way to do this, here's one using sed:

sed -i.bak 's/Daniel/Danny/g' <textfile>
I may be wrong as I am still learning sed but isn't the g option in sed irrelevant. As far as I know, g switch is used for global and sed by default does global changes. g switch is needed for ed(sed's ancestor), which by default only changes the current line. Sed on the other hand changes all the lines by default.

Tux,
 
Old 01-20-2006, 10:58 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
Hi,

You do need the g if you want to change multiple on the same line (global for that line):

cat infile
Daniel aap noot Daniel

sed 's/Daniel/Danny/' infile
Danny aap noot Daniel

sed 's/Daniel/Danny/g' infile
Danny aap noot Danny

Cannot find this in the manpage, but the Sed&Awk book from O'Reilly (page 80 if you have a copy) also states that 'Normally only the first occurence is replaced.'.

Hope this clears things up.
 
Old 01-20-2006, 11:52 AM   #6
tuxrules
Senior Member
 
Registered: Jun 2004
Location: Chicago
Distribution: Slackware64 -current
Posts: 1,152

Rep: Reputation: 60
Quote:
Originally Posted by druuna
Hi,

You do need the g if you want to change multiple on the same line (global for that line):

cat infile
Daniel aap noot Daniel

sed 's/Daniel/Danny/' infile
Danny aap noot Daniel

sed 's/Daniel/Danny/g' infile
Danny aap noot Danny

Cannot find this in the manpage, but the Sed&Awk book from O'Reilly (page 80 if you have a copy) also states that 'Normally only the first occurence is replaced.'.

Hope this clears things up.
Awesome...thanks buddy. As I said, I am still learning. I have that Sed & Awk (O'Reilly) book and now that you've told me I think I remember it. Anyway, I better get back to that book . Thanks for clearing that up.

Tux,
 
Old 01-20-2006, 02:58 PM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
I hasten to point out that almost any text editor or word processor has a search and replace function.
CLI is NOT always the easiest way to do something---although you will certainly learn more that way.
 
  


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
a command/script for replacing a word with another jaakkop Programming 14 12-04-2005 10:21 AM
Microsoft Word won't word wrap Micro420 General 1 06-13-2005 04:36 PM
[BASH] How to filter characters away? !{param#word} {param%word}! Dark Carnival Programming 8 03-17-2005 01:49 PM
sed help, replacing a letter with a word GridX Linux - Newbie 1 09-24-2003 10:21 AM
Viewers for Word, Word Perfect, etc. Claude Williams Linux - General 2 11-22-2001 04:01 AM

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

All times are GMT -5. The time now is 07:12 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration