LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 09-28-2010, 03:28 PM   #1
blueAlien
Member
 
Registered: May 2005
Location: Seattle, WA
Distribution: Slackware 13
Posts: 87

Rep: Reputation: 15
AWK find and replace URL


I'm creating a bash script that will allow for me to move my database between dev, stage and prod servers. I already have a script that backs up the database to a compressed file, which is what I am using for the find and replace. My only problem is that I can't figure out how to escape the url I am searching for.

Code:
strFrom="dev\.domain\.com"
strTo="stage\.domain\.com"

gunzip < $1 | awk 's/$strFrom/$strTo/g' | gzip -c > /srv/admin/database-$date.sql.gz
Returns:
Code:
awk: s/dev\.domain\.com/stage\.domain\.com/g
awk:      ^ backslash not last character on line
Cany anyone please tell me where I am going wrong.

Thanks!
 
Old 09-29-2010, 01:07 AM   #2
murugesan
Member
 
Registered: May 2003
Posts: 149

Rep: Reputation: 28
You need to use sed instead of using awk for the format you mentioned
Use
Code:
sed 's/'"$strFrom"'/'"$strTo"'/g'
 
Old 09-29-2010, 01:33 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

The mixing of quotes isn't needed:
sed "s/$strFrom/$strTo/"

You also do not need to escape the dots in the replacement string (strTo="stage.domain.com").

Hope this helps.
 
Old 09-29-2010, 11:37 AM   #4
blueAlien
Member
 
Registered: May 2005
Location: Seattle, WA
Distribution: Slackware 13
Posts: 87

Original Poster
Rep: Reputation: 15
Thanks!

Just wanted to say thanks for the help! The following code works like a charm.

Code:
strFrom="dev.domain.com"
strTo="stage.domain.com"

gunzip < $1 | awk "s/$strFrom/$strTo/g" | gzip -c > /srv/admin/database-$date.sql.gz
 
Old 09-29-2010, 12:34 PM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,
Quote:
Originally Posted by blueAlien View Post
The following code works like a charm.
Code:
strFrom="dev.domain.com"
strTo="stage.domain.com"

gunzip < $1 | awk "s/$strFrom/$strTo/g" | gzip -c > /srv/admin/database-$date.sql.gz
This works??? I really doubt that. This is not a legal command: awk "s/$strFrom/$strTo/g"

I hope you meant: sed "s/$strFrom/$strTo/g"
 
Old 09-29-2010, 09:07 PM   #6
murugesan
Member
 
Registered: May 2003
Posts: 149

Rep: Reputation: 28
Quote:
Originally Posted by blueAlien View Post
Just wanted to say thanks for the help! The following code works like a charm.

Code:
strFrom="dev.domain.com"
strTo="stage.domain.com"

gunzip < $1 | awk "s/$strFrom/$strTo/g" | gzip -c > /srv/admin/database-$date.sql.gz
If you could have used awk instead of using sed,
check the file size
Code:
gunzip /srv/admin/database-......sql.gz
ls -l /srv/admin/database-......sql
to be of zero bytes.
 
  


Reply

Tags
awk, replace, url


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Find URL in Debian package index via awk/sed (=find a line, then search from there) maddes.b Linux - Software 11 06-28-2013 07:37 AM
awk, sed find and replace recursively from files bluewind Linux - Newbie 17 02-26-2010 10:06 AM
How do I replace a url in the body of an post or email with 'Here'....? elcartero Linux - Newbie 2 08-05-2008 12:19 PM
URGENT: Help for Find and Replace with AWK onacorpuscle Linux - Newbie 3 12-07-2007 11:22 AM
Use sed to find and replace a url xmrkite Linux - Software 4 10-10-2007 07:20 PM


All times are GMT -5. The time now is 06:37 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration