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
# 8  
Old 03-23-2009
Quote:
Originally Posted by slothario
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?
then use the code that i have givenSmilie
# 9  
Old 03-24-2009
Vidyadhar85, your code didn't seem to work for me. For one, it didn't look in any deeper directories (fixed with grep -lr) and also, the temp file is always blank, so it basically erases the contents of the file. How do I fix it?
# 10  
Old 03-24-2009
Quote:
Originally Posted by slothario
Vidyadhar85, your code didn't seem to work for me. For one, it didn't look in any deeper directories (fixed with grep -lr) and also, the temp file is always blank, so it basically erases the contents of the file. How do I fix it?
sorry i made a small mistake
Code:
 
grep -l "<title>" *.htm*>file_list.txt
while read file_name ; do
sed 's/\<title\>/&My Website Name - /g' $file_name > temp_file
mv temp_file $file_name
done < file_list.txt

now it should workSmilie
# 11  
Old 03-24-2009
Actually, I just noticed that myself, but I still seem to be having problems. For some reason when I try to search recursively through the directory, it won't work for *.htm*, but it will for just *. See the output I listed below. What gives?

vine8:/afs/ir.stanford.edu/group/hopes/WWW/test> grep -l -r "<title>" *.htm*
test.html
vine8:/afs/ir.stanford.edu/group/hopes/WWW/test> grep -l -r "<title>" *
findnreplace.sh
test/form.php
test/mailer.php
test/test.html
test.html
# 12  
Old 03-24-2009
Quote:
Originally Posted by slothario
Actually, I just noticed that myself, but I still seem to be having problems. For some reason when I try to search recursively through the directory, it won't work for *.htm*, but it will for just *. See the output I listed below. What gives?

vine8:/afs/ir.stanford.edu/group/hopes/WWW/test> grep -l -r "<title>" *.htm*
test.html
vine8:/afs/ir.stanford.edu/group/hopes/WWW/test> grep -l -r "<title>" *
findnreplace.sh
test/form.php
test/mailer.php
test/test.html
test.html
thats because as you see in grep man page whenever you use -r or -R option it expect a dir if you give * it will go through the child dir also but when you specify a file or group of file it will search in . i.e home dir only
 
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