LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 02-11-2011, 04:04 PM   #1
sneakyimp
Senior Member
 
Registered: Dec 2004
Posts: 1,056

Rep: Reputation: 78
command to find/replace in a text file?


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.
 
Old 02-11-2011, 04:09 PM   #2
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
This may help.
http://tldp.org/LDP/abs/html/string-manipulation.html

Make a script to read each line, look for your target phrase, and if found then apply the change.

There are numerous examples in previous LQ threads. Use the LQ web based search to find them.

Last edited by stress_junkie; 02-11-2011 at 04:10 PM.
 
Old 02-11-2011, 05:03 PM   #3
someshpr
Member
 
Registered: Jul 2009
Location: WI, USA
Distribution: Debian 8, Ubuntu 16.04, CentOS 7
Posts: 143

Rep: Reputation: 28
Quote:
Originally Posted by sneakyimp View Post
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.

HTH,
 
Old 02-11-2011, 11:08 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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.
 
Old 02-11-2011, 11:35 PM   #5
micxz
Senior Member
 
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Rep: Reputation: 75
Could also use perl:
perl -p -i.bak -e 's/oldstring/newstring/g' file
 
Old 02-12-2011, 01:17 PM   #6
sneakyimp
Senior Member
 
Registered: Dec 2004
Posts: 1,056

Original Poster
Rep: Reputation: 78
Thank you for the good info all around.

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?
 
Old 02-12-2011, 01:24 PM   #7
sneakyimp
Senior Member
 
Registered: Dec 2004
Posts: 1,056

Original Poster
Rep: Reputation: 78
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.
 
Old 02-12-2011, 01:33 PM   #8
sneakyimp
Senior Member
 
Registered: Dec 2004
Posts: 1,056

Original Poster
Rep: Reputation: 78
FYI, trying the "I" flag and I'm getting error:
Code:
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$
 
Old 02-12-2011, 02:59 PM   #9
micxz
Senior Member
 
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Rep: Reputation: 75
Here is somethin:
http://www.zytrax.com/tech/web/regex.htm
 
Old 02-12-2011, 03:40 PM   #10
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by sneakyimp View Post
FYI, trying the "I" flag and I'm getting error:
Code:
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:
Code:
perl -i.bak -pe 's/d1.com/d2.com/gi' dump.sql
 
Old 02-13-2011, 04:07 PM   #11
micxz
Senior Member
 
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Rep: Reputation: 75
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.
 
Old 02-14-2011, 01:58 PM   #12
sneakyimp
Senior Member
 
Registered: Dec 2004
Posts: 1,056

Original Poster
Rep: Reputation: 78
Quote:
Originally Posted by micxz View Post
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.
 
Old 02-14-2011, 02:15 PM   #13
sneakyimp
Senior Member
 
Registered: Dec 2004
Posts: 1,056

Original Poster
Rep: Reputation: 78
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.
 
Old 02-14-2011, 09:44 PM   #14
micxz
Senior Member
 
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Rep: Reputation: 75
Glad it's working out. Try:
Code:
sed --version
 
Old 02-14-2011, 11:22 PM   #15
sneakyimp
Senior Member
 
Registered: Dec 2004
Posts: 1,056

Original Poster
Rep: Reputation: 78
Code:
My_Mac:import sneakyimp$ sed --version
sed: illegal option -- -
usage: sed script [-Ealn] [-i extension] [file ...]
       sed [-Ealn] [-i extension] [-e script] ... [-f script_file] ... [file ...]
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Linux command to find and replace string within text file chips11 Linux - Newbie 5 11-24-2008 02:25 PM
Find and replace text in multiple file Bad_Bob Linux - Software 9 05-08-2008 02:31 AM
Need command to search and replace text in file acascianelli AIX 12 04-11-2007 08:16 PM
command line edit -- global find/replace on text file w/o going into vi car182 Linux - Newbie 4 05-25-2006 05:42 PM
help! Script or command needed to replace text in a file. farmerjoe Linux - Newbie 2 01-02-2005 03:07 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration