Knowledgebase

string search and replace

Posted by Woooo, 01-08-2008, 04:33 PM
Hi, I am trying to replace a string from my webfiles, The command I am giving is showing an error, Where is the fault? All I have to do is swap a company name from footer of files in various websites hosted on a server, say I have to swap the word srtingone with stringtwo, I am giving this command for it: But it throws an error : What should i edit to have this change happen in all files in sub-directories of /home folder? Please linux gurus.

Posted by david510, 01-08-2008, 11:06 PM
If the string is unique and if you are sure it will occur only in the user web files, you can follow the steps below. Once you run the first two commands check the files listed in the /root/list.txt and make sure you are going to edit the correct files.

Posted by bitserve, 01-08-2008, 11:34 PM
This isn't a linux question, but a perl one. Using the -pi switches you're telling perl to iterate over filenames and edit them in place. A directory isn't a filename. You'll need to give it some filenames to iterate over. Normally this is done with find, but could even be down with ls -1. I'm guessing you want somethine like: perl -pi -e 's|find|replace|g' `find /home -type f -name "*.html"` First do this to make sure it's the right files: find /home -type f -name "*.html" -print You may want to do a man perlrun for more info.

Posted by activelobby4u, 01-09-2008, 01:58 AM
if you are trying to replace from command line ..use sed instead ---- sed 's///g' file_name > new_file_name cp file_name file_name.bak mv new_file_name file_name -----

Posted by david510, 01-09-2008, 03:42 AM
Sandy, He does not know which all files contains the pattern. So need to get that list first.

Posted by Woooo, 01-09-2008, 11:57 AM
wow this is again amazing David, It worked in a flash what you indicated, Thanks a lot Mark and Sandy for giving solution but it worked with the first resolution already.

Posted by smrtalex, 03-09-2008, 12:36 AM
Any way to make this search ONLY .html files instead of all files? I know I could edit the list.txt file, but there are hundreds of files and rather not have to go the manual route!

Posted by david510, 03-09-2008, 03:04 AM
Rewrite code as follows

Posted by smrtalex, 03-09-2008, 07:27 AM
Sorry.. I think I didn't phrase my question properly. I need to get a listing of all .html files that contain 'stringone' so that then I can replace 'stringone' with 'stringtwo'. Thanks!

Posted by david510, 03-10-2008, 12:28 AM
To get the list. cd /path/to/directory/to/search grep -rl stringone *.html

Posted by smrtalex, 03-13-2008, 12:29 PM
I know there are files that contain my string, but this command is telling no files found. Basically, I am trying to get a listing of ALL .html files in ALL directories in /usr/files with a specific string. I am using: cd /usr/files grep -rl 'http://www.domain.name/cgi-bin/clicks.cgi?XX=666-88880&RR=99999' *.html > /usr/list.txt And I get the following output: grep: *.html: No such file or directory Any thoughts or suggestions?



Was this answer helpful?

Add to Favourites Add to Favourites

Print this Article Print this Article

Also Read
creditcard alternative (Views: 583)


Language:

Client Login

Email

Password

Remember Me

Search