Find and replace in all files using shell scripting


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Find and replace in all files using shell scripting
# 1  
Old 03-23-2009
Find and replace in all files using shell scripting

Hi all,

I'm looking to find and replace a string in all HTML files within a certain directory, including subdirectories. Normally, I would play with this a little to get it to work, but I can't mess this up, so I'm going to ask here.

Basically, I want to find "<title>" in all *.htm* files (to include .htm and .html) and replace it with "<title>My Website Name - ". I'm doing a bit of search engine optimization. How should I write a shell script to do this?

Best,
Danny
# 2  
Old 03-23-2009
do you have "-i" option in your sed man page??
if not first do
Code:
grep -l "<title>" *.htm*>file_list.txt
while read file_name ; do
sed 's/\<title\>/&My Website Name - /g' $filename > temp_file
mv temp_file $file_name
done < file_list.txt

if you have -i option use only sed command

Last edited by vidyadhar85; 03-23-2009 at 07:02 PM..
# 3  
Old 03-23-2009
I do have -i. What do you mean by "use only sed command?"
# 4  
Old 03-23-2009
sed -i 's/pattern/replacestring/g' filename
# 5  
Old 03-23-2009
I'm sorry, I still don't understand how you use that to replace "<title>" in all *.htm* files within a directory and its subdirectories.
# 6  
Old 03-23-2009
Quote:
Originally Posted by slothario
I'm sorry, I still don't understand how you use that to replace "<title>" in all *.htm* files within a directory and its subdirectories.

First cd to the directory where your html files reside, then to change only the first occurrence of <title> in every record run:

Code:
find . -name '*.htm*' -print0 | xargs -0 sed -i 's/<title>/&My Website Name -/'

For every occurrence of <title>...

Code:
... sed -i 's/<title>/&My Website Name -/g'


This will change the files in-place, so try the code first with some sample files.
# 7  
Old 03-23-2009
Okay, this works great on my local machine (running linux). But after logging into the web server, I find the sed on that machine doesn't have the -i switch. What should I do?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace character in shell scripting

Hi, I need to replace the space " " with underscore "_" using shell scripting. The data are inside the text file. Is there are any simple code to that.? (3 Replies)
Discussion started by: gopishrine
3 Replies

2. Shell Programming and Scripting

Search and replace in shell scripting

I am trying to write shell script to find and replace using Sed. but i am unable to complete the setting. need help in doing that. Requirement: FROM "${O_INSTANCE}/diag/logs/${C_TYPE}/${C_NAME}/httpd.pid" TO "/var/opt/<SID>_<HOSTNAME>/Apache/httpd.pid" (10 Replies)
Discussion started by: avmk0407
10 Replies

3. Shell Programming and Scripting

Find and Replace in UNIX Scripting

Hi, Need your advices, Input : select code,status,input from VIEW1.VIEWNAME where IDU_CD IN ('S','N') and status_col='derived')) union select code,status,input from VIEW1.VIEWNAME2 where date='#p1' Expected output : select code,status,input from VIEW1.VIEWNAME where... (2 Replies)
Discussion started by: Nandy
2 Replies

4. Shell Programming and Scripting

Shell script to find and replace contents of files in directory

Hi all This is my first post. Please bear with me with all my mistakes. I started learning shell since couple of days now and this might be quite basic for all, i want to search for files in a directory containing specific string and replace it with new string. The code i wrote is quite bulky... (2 Replies)
Discussion started by: theprogrammer
2 Replies

5. Shell Programming and Scripting

Scripting a global find and replace in an VME output print file

Hi Folks, Below is an extract from a VME Print file which gets handed over to a print house. The problem I have is not that tricky rther looking for a way to handle it in a simple and clean way. Is to first select all lines with "0058" which have four spaces so "0058 " as the selcetion... (3 Replies)
Discussion started by: Gary Hay
3 Replies

6. Shell Programming and Scripting

how to find files and replace them in a directory in Shell scripting

I have a directory /java/unix/data In data directory i have so many files from which i want to find some files who look alike below.(there are number of such below such files as well different files too in the data directory) -68395#svg.xml -56789#ghi.xml -67894#gjk.org -56734#gil.txt I... (6 Replies)
Discussion started by: pratima.kumari
6 Replies

7. Shell Programming and Scripting

Need help with scripting (find and replace)

Hello. I've got to files (source and target). 1) target.txt: .... #: tracopt/mimeview/php.py:97 msgid "" ... (1 Reply)
Discussion started by: Naar
1 Replies

8. Homework & Coursework Questions

[Scripting]Find & replace using user input then replacing text after

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: (o) Checkout an auto part: should prompt the user for the name of the auto part and borrower's name: Name:... (2 Replies)
Discussion started by: SlapnutsGT
2 Replies

9. Shell Programming and Scripting

shell script to find and replace string in multiple files

I used the following script cd pathname for y in `ls *`; do sed "s/ABCD/DCBA/g" $y > temp; mv temp $y; done and it worked fine for finding and replacing strings with names etc. in all files of the given path. I'm trying to replace a string which consists of path (location of file) ... (11 Replies)
Discussion started by: pharos467
11 Replies

10. Shell Programming and Scripting

Find and Replace in multiple files (Shell script)

hi guys, Suppose you have 100 files in a folder and you want to replace all occurances of a word say "ABCD" in those files with "DCBA", how would you do it ??? jatin (13 Replies)
Discussion started by: jatins_s
13 Replies
Login or Register to Ask a Question