LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-05-2004, 05:05 AM   #1
fr0st
LQ Newbie
 
Registered: Apr 2003
Location: Cheshire - United Kingdom
Distribution: RedHat, SuSE, Slackware, EnGarde
Posts: 11

Rep: Reputation: 0
[Bash] Find occurences in a file, then change them


Hi Guys,

I've got a website in a directory that contains a lot of files, and the name of the site has changed slightly. In the PHP files as HTML is the site name.. Now I want to change it from what it is to something else.. I've been using the following code to find all occurences within all the web files and print them to a file... How can I modify this to find the line and the change it?

Code:
find . -name "*.php" -exec fgrep WebSItENamE {} \; > test
I want WebSItENamE to change to WebSiteName. Sounds simple I guess and I would do it by hand but at last count, theres over a 1000 changes to be made and I don't have the time to do that...

Any help would be most appreciated...
 
Old 04-05-2004, 09:09 AM   #2
hw-tph
Senior Member
 
Registered: Sep 2003
Location: Sweden
Distribution: Debian
Posts: 3,032

Rep: Reputation: 58
I suck at sed but it should be the right tool for the job. Something like this to do it recursively perhaps?
Code:
for file in `find . -name \* -type f`; do sed s/WebSItENamE/WebSiteName/ $file >$file.new; mv -f $file.new $file; done

Håkan

Last edited by hw-tph; 04-05-2004 at 09:10 AM.
 
Old 04-05-2004, 09:11 AM   #3
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
Of course you should first make a backup of all *.php files.

WARNING: Untested code follows.
for i in *.php;do sed 's/WebSItENamE/WebSiteName/g' $i> $i.bak;mv -f $i.bak $i;done
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find and change a specific text in a text file by using shell script Bassam Programming 1 07-18-2005 07:15 PM
BASH: How to change a line in file? gmitra Programming 4 01-28-2005 07:26 AM
Change Text-File through bash script arkus Programming 2 12-17-2004 08:48 PM
C++ STL count() number of substring occurences... nyk Programming 4 06-25-2004 07:12 AM
linux command error message bash: /usr/bin/find: No such file or directory sundaram123 Linux - General 8 04-02-2002 07:18 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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