LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-01-2005, 11:58 AM   #1
jaakkop
Member
 
Registered: Aug 2004
Posts: 433

Rep: Reputation: 30
a command/script for replacing a word with another


...

Last edited by jaakkop; 02-28-2011 at 04:44 AM.
 
Old 12-01-2005, 12:13 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
sed

Your example is the most basic use of this very powerful command.
Here's the general form:

sed 's/oldword/newword/g' <oldfile >newfile

This reads from oldfile and replaces ALL occurences of oldword with newword. It then writes to newfile.
You are now standing on the tip of a very large iceberg---here is the best tutorial on sed that I have ever seen: http://www.grymoire.com/Unix/Sed.html

Good Luck
 
Old 12-01-2005, 12:14 PM   #3
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
try:
Code:
sed s/word/new-word/g  file.txt >> new-file.txt
'man sed' for more info.
_______________

sorry pixel, 'mid-air collision'

(you posted right before i hit submit)

Last edited by schneidz; 12-01-2005 at 12:16 PM.
 
Old 12-01-2005, 12:16 PM   #4
reddazz
LQ Guru
 
Registered: Nov 2003
Location: N. E. England
Distribution: Fedora, CentOS, Debian
Posts: 16,298

Rep: Reputation: 77
You can use sed. e.g.
Code:
$sed -e "s/oldword/newword/g" filename > filename2
The syntax maybe slightly different on Linux. I ran that on FreeBSD and it works fine.
 
Old 12-01-2005, 12:17 PM   #5
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Or to edit the file 'in place'
Code:
sed -i -e 's/oldword/newword/' filename
 
Old 12-01-2005, 12:20 PM   #6
reddazz
LQ Guru
 
Registered: Nov 2003
Location: N. E. England
Distribution: Fedora, CentOS, Debian
Posts: 16,298

Rep: Reputation: 77
Got beaten to it. There are many helpful people around here.
 
Old 12-02-2005, 11:03 AM   #7
jaakkop
Member
 
Registered: Aug 2004
Posts: 433

Original Poster
Rep: Reputation: 30
...

Last edited by jaakkop; 02-28-2011 at 04:44 AM.
 
Old 12-02-2005, 12:12 PM   #8
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Use other separators ?
Code:
sed -i "s#word#$INSTDIR#g" file.sh
 
Old 12-02-2005, 01:11 PM   #9
jaakkop
Member
 
Registered: Aug 2004
Posts: 433

Original Poster
Rep: Reputation: 30
...

Last edited by jaakkop; 02-28-2011 at 04:44 AM.
 
Old 12-02-2005, 03:12 PM   #10
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
If you need to include shell variable in the patterns, use double quotes. You can use pretty much any character as the pattern delimiter, the first character after the 's' is taken to be the delimiter. To use the delimiter character in a pattern prefix it with a backslash - you can write some pretty confusing patterns this way!
 
Old 12-02-2005, 04:53 PM   #11
linmix
Senior Member
 
Registered: Jun 2004
Location: Spain
Distribution: FC5
Posts: 1,993
Blog Entries: 1

Rep: Reputation: 46
Quote:
Originally posted by pixellany

here is the best tutorial on sed that I have ever seen: http://www.grymoire.com/Unix/Sed.html

Good Luck
Great link! I'm trying to get to grips with sed mysel and it looks like I might learn some nice tricks there.

I might even learn why sed doesn't appear to work correctly with examples I have from the book in my sig.
 
Old 12-03-2005, 10:01 PM   #12
zahadumy
Member
 
Registered: May 2005
Location: Cluj, Romania
Distribution: Fedora Core 6
Posts: 226

Rep: Reputation: 31
Quote:
Originally Posted by pixellany
here is the best tutorial on sed that I have ever seen: http://www.grymoire.com/Unix/Sed.html
Do you have any links with tutorials?
This tutorial is great and I would be glad to read any tutorial like this, if you have more links... Thank you.
 
Old 12-04-2005, 07:02 AM   #13
linmix
Senior Member
 
Registered: Jun 2004
Location: Spain
Distribution: FC5
Posts: 1,993
Blog Entries: 1

Rep: Reputation: 46
Browse through the parent directory of that link. There's a couple more:
http://www.grymoire.com/Unix/
 
Old 12-04-2005, 10:17 AM   #14
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
I don't generally keep track of links like this--it is so much easier to get it thru Google.
For example, when I search on "sed", I see something like Grymoire and recognize it.

Like Microsoft, Google is making us soft and lazy. I guess the big difference is that we don't have to PAY Google to do this for (to?) us......
 
Old 12-04-2005, 10:21 AM   #15
zahadumy
Member
 
Registered: May 2005
Location: Cluj, Romania
Distribution: Fedora Core 6
Posts: 226

Rep: Reputation: 31
Quote:
Originally Posted by pixellany
I don't generally keep track of links like this--it is so much easier to get it thru Google.
Let's say you search some "sed" examples. You will find through Google the same few simple examples on almost every site. This becomes annoying after a time. That's why I like to have bookmarks, links where you find everything, or at least a lot of documentation. I made a bookmark for sed and awk on that site and I'm pretty sure I will never lose time searching for these 2.
 
  


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
Shell Script: Find "Word" Run "Command" granatica Linux - Software 5 07-25-2007 07:42 AM
Replacing a particular position in a file thru shell script in Linux sgracelin Programming 7 06-22-2005 07:04 AM
when booting, just show GRUB word not command ratana Red Hat 1 05-06-2004 03:40 AM
mp3 files - replacing blank spaces with _ script? mymojo Linux - Newbie 4 12-08-2003 03:33 AM
sed help, replacing a letter with a word GridX Linux - Newbie 1 09-24-2003 10:21 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:59 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