LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 04-12-2003, 08:09 AM   #1
TheSpecial
LQ Newbie
 
Registered: Jan 2003
Posts: 4

Rep: Reputation: 0
Grep for search, but what for replace?


Hi,

I want a tool just like grep (which finds a string in several files), but this should replace a string I specify by another, is there such a program?

Thanks!
 
Old 04-12-2003, 08:23 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,284
Blog Entries: 54

Rep: Reputation: 2854Reputation: 2854Reputation: 2854Reputation: 2854Reputation: 2854Reputation: 2854Reputation: 2854Reputation: 2854Reputation: 2854Reputation: 2854Reputation: 2854
If you don't want to script grep+sed, then check laffeycomputer.com for "rpl".
 
Old 04-12-2003, 06:27 PM   #3
cuckoopint
Member
 
Registered: Feb 2003
Distribution: Debian
Posts: 797

Rep: Reputation: 30
sed or pearl

; )
 
Old 04-12-2003, 07:01 PM   #4
m0rl0ck
Member
 
Registered: Nov 2002
Distribution: A totally 133t distro :)
Posts: 358

Rep: Reputation: 31
perl -p -i -e 's/this/that/g' filename


At the commandline will replace "this" with "that" in filename.
filename coul be *.html or * or whatever.

Last edited by m0rl0ck; 04-12-2003 at 07:04 PM.
 
Old 04-13-2003, 02:19 AM   #5
TheSpecial
LQ Newbie
 
Registered: Jan 2003
Posts: 4

Original Poster
Rep: Reputation: 0
Thx for the information!!
 
Old 04-24-2003, 02:35 PM   #6
TheSpecial
LQ Newbie
 
Registered: Jan 2003
Posts: 4

Original Poster
Rep: Reputation: 0
Oki, I tried it out, but I got onto a problem...

I execute:
perl -p -i -e "s#\$ENV\{\'DOCUMENT_ROOT\'\}#\"/home/kenny/public_html/\"#" *.cgi

And the file now contains (instead of $ENV{'DOCUMENT_ROOT'})$ENV"/home/kenny/public_html/", how can I get rid of the $ENV? I tought that \$ should skip it?

Also another question, is it possible to search recursivly in the subdirectorys?

Thanks!
Kenny
 
Old 04-24-2003, 04:13 PM   #7
BigNate
Member
 
Registered: Mar 2003
Location: Harrisburg, PA
Distribution: Gentoo, Debian, Ubuntu, Red Hat/CentOS
Posts: 719

Rep: Reputation: 30
grep takes the -r (recursive option)

man grep for details
 
Old 04-24-2003, 04:18 PM   #8
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,397

Rep: Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963
using perl is quite pointless i'd say... the same regex should work fine with sed.
 
Old 04-24-2003, 04:28 PM   #9
GAVollink
Member
 
Registered: Apr 2002
Location: St. Paul, Minnesota
Distribution: UbuntuStudio, Ubuntu
Posts: 357

Rep: Reputation: 31
try this then...

find . -name "*.cgi" -exec perl -p -i -e 's/this/that/g' {} \;

As far as your environment variable switch-out - I can't tell what you were trying to change, and what you were trying to change it to. I'd be more helpful otherwise.

(I think perl can do this better when coupled with find - but I am probably not aware of some flag that would ask 'sed' to read from a file instead of a stream)

Last edited by GAVollink; 04-24-2003 at 04:32 PM.
 
Old 04-24-2003, 07:43 PM   #10
cuckoopint
Member
 
Registered: Feb 2003
Distribution: Debian
Posts: 797

Rep: Reputation: 30
Quote:
but I am probably not aware of some flag that would ask 'sed' to read from a file instead of a stream
Do you mean like:
Code:
sed [OPTION]... {script-only-if-no-other-script} [input-file]...
 
Old 04-24-2003, 07:57 PM   #11
arnold
Member
 
Registered: Dec 2002
Posts: 226

Rep: Reputation: 30
i favor ex with here script
ex *.cgi<<!
%s/this/that/g
%s/abc/def/g
w
q
!

PS no matter which way, u must test and backup files b4 making a production run
 
Old 04-24-2003, 08:01 PM   #12
m0rl0ck
Member
 
Registered: Nov 2002
Distribution: A totally 133t distro :)
Posts: 358

Rep: Reputation: 31
Quote:
how can I get rid of the $ENV? I tought that \$ should skip it?
I just tried:
perl -p -i -e 's/\$ENV//g
on a test file and it worked great.
 
Old 04-24-2003, 08:15 PM   #13
m0rl0ck
Member
 
Registered: Nov 2002
Distribution: A totally 133t distro :)
Posts: 358

Rep: Reputation: 31
Quote:
using perl is quite pointless i'd say... the same regex should work fine with sed.
Yeah, "sed -e 's/this/that/g' filename" works great.
I'm afraid this may be a case of "if the only tool you have is a hammer" Im used to using perl as a first resort to solve just about any system/admin problem. Its probably a failing, but perl is such a darn good hammer
 
Old 04-25-2003, 07:29 AM   #14
arnold
Member
 
Registered: Dec 2002
Posts: 226

Rep: Reputation: 30
sed is "stream editor", not file editor. If you want to directly modify a file, you need perl or ex.

perl and ex can do the job in 1 command, sed requires at least 2 commands - sLedGE hammer
 
Old 04-25-2003, 10:57 PM   #15
m0rl0ck
Member
 
Registered: Nov 2002
Distribution: A totally 133t distro :)
Posts: 358

Rep: Reputation: 31
Quote:
perl and ex can do the job in 1 command, sed requires at least 2 commands

But "sed -e 's/this/that/g' *" gives exactly the same result as the perl one liner and its fewer characters.
 
  


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
global search and replace? jkcunningham Linux - General 5 03-15-2006 02:43 PM
search and replace in every file Thinking Programming 3 07-30-2005 05:50 PM
How to search and replace a text using grep DediPlace Linux - General 2 05-29-2005 06:47 PM
find with grep and replace it dominant Linux - Newbie 4 03-03-2004 01:11 PM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 01:04 AM


All times are GMT -5. The time now is 05:22 PM.

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