LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   AWK find and replace URL (https://www.linuxquestions.org/questions/linux-general-1/awk-find-and-replace-url-835021/)

blueAlien 09-28-2010 03:28 PM

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!

murugesan 09-29-2010 01:07 AM

You need to use sed instead of using awk for the format you mentioned
Use
Code:

sed 's/'"$strFrom"'/'"$strTo"'/g'

druuna 09-29-2010 01:33 AM

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.

blueAlien 09-29-2010 11:37 AM

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


druuna 09-29-2010 12:34 PM

Hi,
Quote:

Originally Posted by blueAlien (Post 4112805)
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"

murugesan 09-29-2010 09:07 PM

Quote:

Originally Posted by blueAlien (Post 4112805)
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.


All times are GMT -5. The time now is 04:29 AM.