?
Solved

Find & Replace All Files URGENT

Posted on 2003-03-26
12
Medium Priority
?
130 Views
Last Modified: 2010-03-05
Hello,

I wanted to know how I am going to find an replace the following files

.pl
.html
.cgi
.txt

Let say I wanted to find $your in all the above files and then replace that with $this

How I am going to do that using my linux redhat 6.2 server .

Thanks.
0
Comment
Question by:sohaib69
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 8210460
perl -pi -e 's/\$your/\$this/g' *.pl *.html *.cgi *.txt
0
 
LVL 1

Author Comment

by:sohaib69
ID: 8210584
ok sir,

as you have mention

perl -pi -e 's/\$your/\$this/g' *.pl *.html *.cgi *.txt


I wanted to find and replce that inside the following folder

/home/admin/www/

How I will do that.
0
 

Expert Comment

by:Sakana
ID: 8210981
cd /home/admin/www/
perl -pi -e 's/\$your/\$this/g' *.pl *.html *.cgi *.txt


He he....
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:sohaib69
ID: 8211065
also one more thing,

I want that command to change all the files cos there are other directories inside /home/admin/www/main/

so how that command will change

perl -pi -e 's/\$your/\$this/g' *.pl *.html

every files going inside other directories too such as

/home/admin/www/ and /home/admin/www/main/

If that cant be done then Do I have to change by going on to every directory.

Thanks
0
 
LVL 3

Accepted Solution

by:
tosch earned 200 total points
ID: 8211327
#!/bin/bash

ext="html pl"


for e in $ext ; do echo $e; for f in `find -type f -name '*'$e` ; do
    perl -pi -e 's/\$your/\$this/gs' $f
done ; done
0
 
LVL 1

Author Comment

by:sohaib69
ID: 8211358
thanks a lot. u are a reall expert also will u help me with changing few things i will pay you for the job you will do for me.

my email is sohaib@sohaib.com email me and let me know.

Thanks.
0
 
LVL 1

Author Comment

by:sohaib69
ID: 8211365
Excellent JOB
0
 
LVL 1

Author Comment

by:sohaib69
ID: 8211384
I have one more question could you help me in finding the files using find command , I wanted to find the following

Testing

let me know how I am going to find that Testing in all files and directories.

Thanks
0
 
LVL 1

Author Comment

by:sohaib69
ID: 8217076
I tried that.

#!/bin/bash

ext="html pl"


for e in $ext ; do echo $e; for f in `find -type f -name '*'$e` ; do
   perl -pi -e 's/\$your/\$this/gs' $f
done ; done


it run fine but didnt change anything .

why is that
0
 
LVL 1

Author Comment

by:sohaib69
ID: 8217123
also i am trying to change like

perl -pi -e 's/\THIS/\WITHTHIS/g' *.pl *.html


Its not changing that too.
0
 
LVL 3

Expert Comment

by:tosch
ID: 8219690
perl -pi -e 's/this/that/g' myfile.txt

this line changes all occurences of the word "this" to "that" in file myfile.txt. matching is case sensitive, this means "this" is not equal to "THIS".
it does not work under cygwin.

ext="html pl"
for e in $ext ; do
   # what stands here will be executed 2 times
   # one time with $e evaluating to html and one time
   # with $e evaluating to pl
done


find -type f -name '*.html' # this lists all regular files with a name ending with "html". probably it won't find all files because of symbolic links (man find for details)

the inner for loop iterates through the entries put out by the find command and executes the code enclosed for each entry
0
 
LVL 3

Expert Comment

by:tosch
ID: 8219698
grep this * # finds the occurence of the word "this" in all files in the current directory (man grep for details)

well, you should be able to build a simple script to recurse directories
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question