Help search and replace issue!


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help search and replace issue!
# 1  
Old 04-25-2005
Help search and replace issue!

I have no idea really what I am doing but I am very good at copy-paste code from smart people Smilie

I found it a while back but can't get it now, I am looking for a simple command line prompt command to search and replace text in multiple files.

I have a website with a ton of pages and all links and such are referenced as 64.92.199.9, but I now want to have eveything under the domain www.honeybearbaskets.com

is there a one-liner (or something simple) that will search and replace the IP with the domain in all the file in the current diriectory?

Thanks for anything you can offer!
# 2  
Old 04-25-2005
if file1 contains 64.92.199.9 replace that by

Code:
sed 's/64.92.199.9/www.honeybearbaskets.com/' file1

and get all the files containing 64.92.199.9 by

Code:
grep "64.92.199.9" *


in the current dir
# 3  
Old 04-25-2005
so I would have to type the sed command for every single file?

isn't there a foreach file (*.htm) kind of command?

Thanks though, it helps!

Last edited by scavok; 04-25-2005 at 07:04 PM.. Reason: typo
# 4  
Old 04-25-2005
Quote:
Originally Posted by scavok
so I would have to type the sed command for every single file?

isn't there a foreach file (*.htm) kind of command?

Thanks though, it helps!
do looping like this ...

Code:
for file in `grep "64.92.199.9" *`
do
   sed 's/64.92.199.9/www.honeybearbaskets.com/' $file > tmp
   mv tmp $file
done


Note : Above code not tested.
# 5  
Old 04-27-2005
I just wanted to post the code I ended up using

Code:
for file in *.htm
do
sed 's/honeybearbasket.com/honeybearbaskets.com/' $file > $file.new
mv $file $file.old
mv $file.new $file
rm $file.old
done

Thank you for your help!
# 6  
Old 04-27-2005
some more optimization based on a code Ygor put in on 1 of the threads here ... keeps the modified file permissions and ownerships same as original file ...
Code:
for file in *.htm
do
    cp $file $file.tmp
    sed 's/honeybearbasket.com/honeybearbaskets.com/' $file.tmp > $file
    rm $file.tmp
done

# 7  
Old 09-19-2006
I am sure this is a known issue. When I changing a string in all xml files, it is truncating the file after it successfully changing the string in all files...

Any workaround?

Here is the code:

for files in *.xml;
do
mv $files $files.old
sed 's/OLD_STRING/NEW_STRING/g' $files.old > $files
rm -f $files.old
done
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Nested search in a file and replace the inner search

Hi Team, I am new to unix, please help me in this. I have a file named properties. The content of the file is : ##Mobile props east.url=https://qa.east.corp.com/prop/end west.url=https://qa.west.corp.com/prop/end south.url=https://qa.south.corp.com/prop/end... (2 Replies)
Discussion started by: tolearn
2 Replies

2. UNIX for Dummies Questions & Answers

Help with search and replace or search only of / in vi

Hi all, I am editing a config file in vi that has a / on it. At the moment, search and replace looks alright as am able to use a # as a temporary separator, i.e. :,$s#/u01/app#/u02/app#g For doing a search, I have to escape the / do. So if I want to search for /u01/app, I am having to do... (2 Replies)
Discussion started by: newbie_01
2 Replies

3. Shell Programming and Scripting

Search and Replace

hi all, I have an xml and which has a section <!ENTITY commonssf1lymphoma SYSTEM "commons/commonssf1lymphoma.xml"> <!ENTITY commonssf2lymphoma SYSTEM "commons/commonssf2lymphoma.xml"> <!ENTITY commonssf3lymphoma SYSTEM "commons/commonssf3lymphoma.xml"> i want to replace... (9 Replies)
Discussion started by: r_t_1601
9 Replies

4. Shell Programming and Scripting

perl search and replace - search in first line and replance in 2nd line

Dear All, i want to search particular string and want to replance next line value. following is the test file. search string is tmp,??? ,10:1 "???" may contain any 3 character it should remain the same and next line replace with ,10:50 tmp,123 --- if match tmp,??? then... (3 Replies)
Discussion started by: arvindng
3 Replies

5. Shell Programming and Scripting

Search and replace

Hi, I have a variable which names the file path, like: ./home/folder1/a/b/c/file.txt the names of the folders, and the number of upper level folders will be different, I want it just to return the name of the first upper directory with "_" then the folder name, with something added... (5 Replies)
Discussion started by: a27wang
5 Replies

6. Shell Programming and Scripting

Need Help With Search And Replace

Hey all, So, my server has recently been hit with an annoying injection. Most of my files have a nasty little iframe attached to them now. What I need to do is a recursive search and replace - which I could write in PHP, but then I'd still have to run it more times than I'd like due to memory... (4 Replies)
Discussion started by: scheda
4 Replies

7. Shell Programming and Scripting

awk - replace number of string length from search and replace for a serialized array

Hello, I really would appreciate some help with a bash script for some string manipulation on an SQL dump: I'd like to be able to rename "sites/WHATEVER/files" to "sites/SOMETHINGELSE/files" within the sql dump. This is quite easy with sed: sed -e... (1 Reply)
Discussion started by: otrotipo
1 Replies

8. Shell Programming and Scripting

search and replace

i have the following create table definition ,the shell script has to search for the first column name of the create table definition in the file and explicitly add primary index (first column name) at the end i.e before ";" it has to search through all the files (say 20 files) and has to... (1 Reply)
Discussion started by: a.suryakumar
1 Replies

9. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies

10. Shell Programming and Scripting

search and replace

hi all, the problem is like this...... i setup a file (Env.txt) which handles all the values. NAME1=xxxxxx, where xxxxx is the value NAME2=xxxxxx GGGGG=uusufu I have 6 files, where i will append the values from env.txt. These files has no specific format. all i want is to append the... (1 Reply)
Discussion started by: tungaw2004
1 Replies
Login or Register to Ask a Question