Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have an SQL dump, file.sql that has many references to a particular domain, d1.com. I would like to run a command that can replace every occurrence of d1.com with d2.com. I've tried looking into sed before but the man pages are quite daunting. Any help would be much appreciated.
I have an SQL dump, file.sql that has many references to a particular domain, d1.com. I would like to run a command that can replace every occurrence of d1.com with d2.com. I've tried looking into sed before but the man pages are quite daunting. Any help would be much appreciated.
I haven't used sed much, but I believe something like this might work:
Code:
sed -i "s/d1.com/d2.com/g" file.sql
I am not sure, but you might have to use the escape character to get the period (.) properly parsed by the command.
As stress_junkie suggested there are numerous of sed related example in LQ. Also when a problem is solved, please mark it as solved.
As an add on to the last post, if you are not sure about the results you can append something like '.bak' after the '-i' option to create a backup.
Check the man page for this option.
stress_junkie, the bash stuff looks really helpful but I'm certainly not much of a shell script writer. Reading the file seems a bit confusing. There is a read example on that page but it's got some confusing comparison operators.
I'm kind of psyched to get more familiar with sed because it seems like a powerful tool. I have considerable familiarity with regular expressions in a PHP context (using preg_match) but don't really recognize the syntax of 's/oldstring/newstring/g'. I need to make this case-insensitive. Can someone explain the format of the expression? Is there somewhere to find documentation on this type of expression?
I found this and it describes how to use the "y" command to change the case of strings but does not describe how to get a case-insensitive search and replace.
prompt$ sed -i .bak "s/d1.com/d2.com/gI" dump.sql
sed: 1: "s/d1.com/d2 ...": bad flag in substitute command: 'I'
prompt$
The 'I' flag is available in GNU sed only. Which version of sed are you running? And which OS? In alternative you can try the suggested perl code (see post #5 by micxz) with a slight modification:
here is a nice one liner or example page: http://sed.sourceforge.net/sed1line.txt
look almost to the end of the file for what colucix means about the GNU versions.
Thanks for that. I'm quite well acquainted with regular expressions generally speaking, but have a lot more experience working with them in PHP. What's puzzling about this SED regex syntax is that it appears to start with a command -- in this case "s" which stands for "substitute" -- and then has three slashes rather than being bracketed by just two slashes. I'm thinking I might need assistance with SED and or PERL regex command syntax rather than basic regex.
colucix: I don't *know* what version of sed I'm using. I've checked the man pages and there doesn't appear to be any way to determine what version I have. I get the feeling from the bits of reading I've done that there are many variations of sed. I am dealing with a variety of machines for this task: OSX 10.5.8, CentOS 5.5, Ubuntu, etc. It would be nice to come up with some kind of scheme that works on all of them with no surprises.
micxz: I read the stuff at the end but I still don't know how to get my sed version or how I might translate that version into a plan of action here.
I also tried perl with the I flag and it complained about a bad modifier.
hm. I'm also having an issue where "somestring com" (note the space) is also being modified when I do a replace on "somestring.com" because the . is being treated as a wildcard. Grep doesn't seem to like it when I add a backslash to escape the period.
I am trying the following on CentOS 5.5 with some good results:
Code:
perl -p -i.bak -e 's/d1\.com/d2.com/gi' file.sql
This also appears to work on OSX -- I tried an uppercase "I" flag before and that caused the complaint.
THANKS for your help, gentlefolk! I'm cooking right along and the extra link resources should help me get better at this. Have a great week.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.