LinuxQuestions.org
Review your favorite Linux distribution.
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 01-03-2006, 07:19 AM   #1
bullshit
Member
 
Registered: Oct 2003
Posts: 44

Rep: Reputation: 15
sed - find and replace command


Hi, I need some help with this one.
I'm tryig to execute the following

sed -e 's/string1/string2/g' filename

The script runs ok and prints to stdout but is not saving the changes to the specified filename. Is that a normal behaviour or I'm missing a command parameter.

Thanks in advance, any help apreciated

Cheers
 
Old 01-03-2006, 07:33 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
it doesn't edit the file in place.
you have to copy, rename etc.

perl can do it in place with optional save to backup file:


Code:
perl -pi.bak -e 's/this/that/g' FILE
 
Old 01-03-2006, 07:53 AM   #3
bullshit
Member
 
Registered: Oct 2003
Posts: 44

Original Poster
Rep: Reputation: 15
Thanks a lot. I will go for it. Didn't you make a typo
perl -pi.bak | sed -e 's/this/that/g' FILE
instead of
perl -pi.bak -e 's/this/that/g' FILE

I'm sorry if I sound stupid. Can you correct me?

Cheers
 
Old 01-03-2006, 08:02 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
nope,
no typo.

no sed involved.
probably safe to leave the .bak in place, just in case.

(perl is pretty much a superset of sed)

Last edited by bigearsbilly; 01-03-2006 at 08:03 AM.
 
Old 01-03-2006, 08:36 AM   #5
bullshit
Member
 
Registered: Oct 2003
Posts: 44

Original Poster
Rep: Reputation: 15
Thanks

Thanks for a great advise, i will try it in few minutes.

Cheers and Happy 2006
 
Old 01-03-2006, 07:03 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,342

Rep: Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746
Here's sed in place ...
sed -i -e 's/DROP TABLE/DROP TABLE IF EXISTS/' ClientInfo_audit.sql
enjoy ...
 
Old 01-04-2006, 04:58 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
unfortunately, there's no -i on your average UNIX sed.
 
Old 01-04-2006, 05:26 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,342

Rep: Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746
ah well, this is a linux forum
 
Old 01-04-2006, 08:55 PM   #9
mhcox
Member
 
Registered: Aug 2005
Location: Albuquerque, NM
Distribution: Fedora
Posts: 30

Rep: Reputation: 15
Quote:
Originally Posted by bullshit
Hi, I need some help with this one.
I'm tryig to execute the following

sed -e 's/string1/string2/g' filename

The script runs ok and prints to stdout but is not saving the changes to the specified filename. Is that a normal behaviour or I'm missing a command parameter.

Thanks in advance, any help apreciated

Cheers
sed -e 's/string1/string2/g' filename >filename.$$; mv -f filename.$$ filename

Although I agree with the post above about using Perl. Forget sed. You can do anything sed can
do in Perl and even understand it latter. Sed "programs" tend to look like line noise :-).

Last edited by mhcox; 01-04-2006 at 08:59 PM.
 
Old 01-05-2006, 03:25 AM   #10
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
Quote:
Originally Posted by mhcox
sed -e 's/string1/string2/g' filename >filename.$$; mv -f filename.$$ filename

Although I agree with the post above about using Perl. Forget sed. You can do anything sed can
do in Perl and even understand it latter. Sed "programs" tend to look like line noise :-).
the above approach has a potential bug of changing the default file permissions of the original file

instead, try

Code:
sed 's/string1/string2/g' filename > filename.$$
cat filename.$$ > filename
rm filename.$$
this would preserve the file permissions also
 
  


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
sed command to replace slash with backslash stelmed Slackware 13 08-19-2010 05:01 AM
SED;find and replace;help required gd13 Programming 3 12-21-2004 06:33 AM
[sed] replace string? chuanyung Programming 3 03-11-2004 08:42 PM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 01:04 AM
find and replace in files from command line dexter_modem Linux - General 4 06-10-2003 11:27 AM

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

All times are GMT -5. The time now is 10:00 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
Open Source Consulting | Domain Registration