LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   help! Script or command needed to replace text in a file. (https://www.linuxquestions.org/questions/linux-newbie-8/help-script-or-command-needed-to-replace-text-in-a-file-272988/)

farmerjoe 01-02-2005 01:31 PM

help! Script or command 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

ttolst 01-02-2005 02:52 PM

ok, this is REALLY quick and dirty, but it should actually get the work done. It is not failsafe by any means, and will happily change \" to \\\" (first \ to \\, then " to \") So the first one, which turns the \ into \\ must be run first. You better not use any of the characters for anything else, or you need a more advanced solution :-) (which i dont have time to try and think up right now)

perl -pi -e 's/\\/\\\\/g' file
perl -pi -e 's/"/\\"/g' file
perl -pi -e "s/\'/\\\'/g" file
perl -pi -e 's/\`/\\\`/g' file

farmerjoe 01-02-2005 03:07 PM

Thanks! that should do the trick! i was having a lot trouble changing the ' character using sed alone. perl worked nicely.

-farmerjoe


All times are GMT -5. The time now is 10:51 AM.