LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-16-2001, 05:07 PM   #1
FraserR
LQ Newbie
 
Registered: Jan 2001
Location: Kent, England
Distribution: SuSE
Posts: 3

Rep: Reputation: 0
Red face


Hello,

I have been trying to find and replace text strings (mainly html links) in multiple html files using grep and say ed or sed by piping the output of grep to the editor.

I can get grep to display the names of the files which contain the offending string, thus

grep -H -e 'Criteria' *.html

BUT, I cant figure how to get, say, ed or sed to use this and replace with another string.

All Help most welcome.

Ritchie
 
Old 06-14-2004, 07:57 AM   #2
arobic
Member
 
Registered: Jul 2003
Location: Geneva, Switzerland
Distribution: Debian 3.1, SLC3 (based on RHEL)
Posts: 84

Rep: Reputation: 15
Considering the date at which you posted this thread, maybe you've figured out how to do this already, but it may be useful for other people having the same question!!

To substitute your string, you don't need to find it using grep, you only need to use sed to find the regexp and substitute it for you, like this:

sed 's/[Criteria]/[Replacementstring]/' [file].html > [newfile].html

Of course, if you want to do this for many files, you'd better do this in a shell script:

#!/bin/sh
for i in foo.html bar.html
do
sed 's/[Criteria]/[Replacementstring]/' $i > $i.new
done
 
Old 08-18-2004, 10:04 PM   #3
bad_lemming
LQ Newbie
 
Registered: Aug 2004
Posts: 4

Rep: Reputation: 0
I am having a similar problem with a directory full of .php files. This sounds like something that would work for me... but could someone explain the code a bit more in depth (edit: or if you could point me to a good read on shell scripts ), and if this will do what I would like?


Code:
#!/bin/sh
for i in foo.html bar.html
do
sed 's/[Criteria]/[Replacementstring]/' $i > $i.new
done

Last edited by bad_lemming; 08-18-2004 at 10:50 PM.
 
Old 08-19-2004, 04:11 AM   #4
arobic
Member
 
Registered: Jul 2003
Location: Geneva, Switzerland
Distribution: Debian 3.1, SLC3 (based on RHEL)
Posts: 84

Rep: Reputation: 15
Hi!

Ok, so the script goes like this:

- I first loop on the different files I have (here foo.html and bar.html).
- Then I ask sed to substitute with the command 's/<old>/<new>/' <filename>. <old> is the string you are looking for and new is the one you want to change for. The filename here is the index of my for loop, i.e. all the files in which I want to make this substitution.
- I then redirect the output of sed to a new file using "> newfile". Otherwise, sed only outputs the result at the stdout without modifying the file.
- That's it!

Maybe you can give me some details of what you are doing so that I may help you in a more efficient way.

A good read on bash shell scripts:
http://www.tldp.org/LDP/abs/html/

According to me, it's the best one out there, complete with examples and detailed explanation.

Have fun!
 
Old 08-19-2004, 09:48 PM   #5
bad_lemming
LQ Newbie
 
Registered: Aug 2004
Posts: 4

Rep: Reputation: 0
OK so then I will have to list all files I want to do the string replace in the "for i in " statement?

E.G.: <For i in fu.php bah.php as.php alwaze.php>

Like that with all the files listed? or is it the first and last file?

other then that it makes sense, thank you so much for your help with a SUPER newb.
 
Old 08-19-2004, 09:58 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
How about this one? :)

Code:
find . -name *.php -type f -exec perl -w -pi -e "s/oldinfo/newinfo/g;" {} \;

Cheers,
Tink
 
  


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
grep ?? can grep us variables? DaFrEQ Linux - Software 4 09-14-2005 12:22 PM
What does rpm -qa |grep th* (as compared to rpm -qa |grep th) display? davidas Linux - Newbie 2 03-18-2004 01:35 AM
"Undeleting" data using grep, but get "grep: memory exhausted" error SammyK Linux - Software 2 03-13-2004 03:11 PM
combining old 486's??? Peppercorn Linux - Software 5 01-22-2004 06:04 PM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 11:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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