The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Perl: Search for string on line then search and replace text Crypto Shell Programming and Scripting 4 01-04-2008 06:24 AM
Search and Replace in Ksh DeepakXavier Shell Programming and Scripting 9 05-28-2007 05:11 AM
sed search and replace d__browne UNIX for Dummies Questions & Answers 7 04-26-2006 06:46 AM
Search and replace sed or tr bridgeje Shell Programming and Scripting 6 10-28-2003 03:54 AM
search and Replace mukeshannamalai UNIX for Advanced & Expert Users 4 09-14-2001 03:21 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-17-2007
Registered User
 

Join Date: Feb 2007
Posts: 20
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
search and replace

I have a I need to replace all instances of the word mysite.com to maysite.net within the public_html web folder. How can I accomplish this.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-17-2007
Shell_Life's Avatar
Unix/Informix/4GL/SQL
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 694
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
dannyd, try this:
Code:
for FileName in `ls -1`
do
 sed 's/mysite.com/mysite.net/g' $FileName > $$TempFile
 mv $$TempFile $FileName
done
Reply With Quote
  #3 (permalink)  
Old 04-17-2007
Registered User
 

Join Date: Feb 2007
Posts: 20
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Would this replace the keyword mysite.com to mysite.net in all subfolders as well ?
Reply With Quote
  #4 (permalink)  
Old 04-17-2007
Shell_Life's Avatar
Unix/Informix/4GL/SQL
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 694
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Quote:
Would this replace the keyword mysite.com to mysite.net in all subfolders as well ?
No. You didn't specify it in your request.
To list all files in your directory and sub-directories, replace
`ls -1` by `find .`
Reply With Quote
  #5 (permalink)  
Old 04-17-2007
Registered User
 

Join Date: Feb 2007
Posts: 20
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Sorry I dont think i was clear in my question. I would like to replace all instances of the word mysite.com to mysite.net inside every file in the entire website.

I think your solution searches for files with the name mysite.com and renames them to mysite.net.
Reply With Quote
  #6 (permalink)  
Old 04-17-2007
Shell_Life's Avatar
Unix/Informix/4GL/SQL
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 694
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
No dannyd,
My solution with the "find" loops thru every single file in the current directory
and sub-directories and for each file found, it searches and replaces the string
"mysite.com" to "mysite.net".
Reply With Quote
  #7 (permalink)  
Old 04-17-2007
Registered User
 

Join Date: Feb 2007
Posts: 20
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Sorry im not too experienced in unix.

Can this command be done on the command line or do i need to put this in a shell script ?
Reply With Quote
  #8 (permalink)  
Old 04-17-2007
Registered User
 

Join Date: Mar 2007
Location: Chennai
Posts: 221
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
No. You didn't specify it in your request.
Quote:
To list all files in your directory and sub-directories, replace
`ls -1` by `find .`
Danny,
I dont think we may require `ls -1` in the for loop in case we are listing the files/directories in the Current working directory just an * will do.

Also note `find .` or `ls -1` or simple * will list all files & directories,so we may to have use,

Quote:
for file in $(find . -name '*' -type f -print ) ; do
#Replacement Logic
done
Please correct me if am wrong.

Thanks
Nagarajan Ganesan
Reply With Quote
  #9 (permalink)  
Old 04-17-2007
Registered User
 

Join Date: Feb 2007
Posts: 20
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
So I guess this would be the final solution ?

for file in $(find . -name '*' -type f -print ) ; do
sed 's/mysite.com/mysite.net/g' $FileName > $$TempFile
mv $$TempFile $FileName
done

If i want to run this command do I type this out on the command line or do i have to make a shell script and run the shell script ?
Reply With Quote
  #10 (permalink)  
Old 04-17-2007
Shell_Life's Avatar
Unix/Informix/4GL/SQL
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 694
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Thank you ennstate,
You are right, the "find ." will list everything, including the directories.
Also, there is no need for "-name '*'" nor "-print".
The "find . -type -f" is sufficient to list all regular files and not directories.
Thanks.
Reply With Quote
  #11 (permalink)  
Old 04-17-2007
Registered User
 

Join Date: Feb 2007
Posts: 20
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Hi,

Do I run this on the command line or do I have to run this command in a script ?
Reply With Quote
  #12 (permalink)  
Old 04-17-2007
Shell_Life's Avatar
Unix/Informix/4GL/SQL
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 694
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
dannyd,
The best way to run this is:
1) Create a file with the above commands.
2) Change its permissions to executable (ie 755, etc.).
3) Type the file name in the command line.
Reply With Quote
  #13 (permalink)  
Old 04-17-2007
Registered User
 

Join Date: Sep 2006
Posts: 1,403
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
no need the for loop
Code:
find . -size +1024c -type f -name "*.txt" -print0 | xargs -0 sed -i 's/term/replace/g'
NB. -i option only for gnu sed.
Reply With Quote
  #14 (permalink)  
Old 04-17-2007
Registered User
 

Join Date: Feb 2007
Posts: 20
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Quote:
Originally Posted by ghostdog74
no need the for loop
Code:
find . -size +1024c -type f -name "*.txt" -print0 | xargs -0 sed -i 's/term/replace/g'
NB. -i option only for gnu sed.
I can run this on the command line, but...

Wouldnt this search only files that end with .txt and would it search subfolders ?
Reply With Quote
  #15 (permalink)  
Old 04-17-2007
Registered User
 

Join Date: Feb 2007
Posts: 20
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Do you think this would work:

find . -size +1024c -type f -name "*" -print0 | xargs -0 sed -i 's/mysite.com/mysite.net/g'
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes


The 50 most popular UNIX and Linux searches.
Google Search Cloud for The UNIX and Linux Forums
421 service not available, remote server has closed connection ^m autosys awk trim bash eval bash exec bash for loop command copy/move folder in unix couldn't set locale correctly curses.h cut command in unix daemon process export command in unix find grep find mtime find null character in a unix file grep multiple lines grep or grep recursive hp-ux ifconfig inaddr_any inappropriate ioctl for device lynx javascript mailx attachment mget mtime ping port remove first character from string in k shell replace space by comma , perl script scp recursive segmentation fault(coredump) sftp script snoop unix stale nfs file handle syn_sent tar exclude tar extract to folder test: argument expected unix unix .profile unix forum unix forums unix internals unix interview questions unix simulator unix.com vi select all vi substitute vi+substitute+end+of+line+character while loop within while loop shell script