LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Help! Script or commanded needed to replace text in a file (https://www.linuxquestions.org/questions/programming-9/help-script-or-commanded-needed-to-replace-text-in-a-file-273011/)

farmerjoe 01-02-2005 02:44 PM

Help! Script or commanded needed to replace text in a file
 
I am relatively new to linux. I need to replace occurences of chars (", ', `, and \) with (\", \', \`, and \\). I do not know of a linux function that will do that, only a PHP one which is str_replace. I need these characters changed to the new ones because PostgreSQL and MySQL dont recognize these characters. Any help i can get would be great. I assume i will have to use sed or gawk but i am completely unfamilier with how those work.

Thanks,
-farmerjoe

jlangelier 01-02-2005 03:12 PM

This works for me... know in the business as a 'perl one-liner'. Perl's kind of touchy on one-liners... it'll probably work for you, but I can't guarantee it.

On the command line:

Code:

perl -i.bak -pe "s/([\"\'\`\\\])/\x5c\$1/g" /tmp/test.txt
... and change /temp/test.txt to the name of the file you want to change. The above one-liner will back up your original file with a .bak extension.

Hko 01-02-2005 03:15 PM

Re: Help! Script or commanded needed to replace text in a file
 
Quote:

Originally posted by farmerjoe
I need these characters changed to the new ones because PostgreSQL and MySQL dont recognize these characters.
For PHP there's a special function to convert strings for use in SQL-queries.
See http://www.php.net/manual/en/functio...ape-string.php
If you're not using PHP, you may find a similar function for your language.

farmerjoe 01-02-2005 05:59 PM

thanks a lot for all your help! the perl script worked nicely! couldnt have done it without you.

-farmerjoe


All times are GMT -5. The time now is 01:13 AM.