LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-02-2005, 02:44 PM   #1
farmerjoe
Member
 
Registered: Oct 2004
Location: Texas
Distribution: Ubuntu - Home, RHEL4 - Server
Posts: 96

Rep: Reputation: 15
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
 
Old 01-02-2005, 03:12 PM   #2
jlangelier
Member
 
Registered: Jun 2003
Location: Denver, CO
Distribution: Debian
Posts: 95

Rep: Reputation: 15
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.
 
Old 01-02-2005, 03:15 PM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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.
 
Old 01-02-2005, 05:59 PM   #4
farmerjoe
Member
 
Registered: Oct 2004
Location: Texas
Distribution: Ubuntu - Home, RHEL4 - Server
Posts: 96

Original Poster
Rep: Reputation: 15
thanks a lot for all your help! the perl script worked nicely! couldnt have done it without you.

-farmerjoe
 
  


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
Replace text of unknown content with other text in file brian0918 Linux - Software 1 07-14-2005 03:22 PM
help! Script or command needed to replace text in a file. farmerjoe Linux - Newbie 2 01-02-2005 03:07 PM
Script to search and replace in text file - kinda... jeffreybluml Programming 45 11-07-2004 05:37 PM
Search and replace text in file using shell script? matthurne Linux - Software 2 11-02-2004 10:11 AM
Script file to replace large text blocks in files? stodge Linux - Software 0 09-27-2003 10:53 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:57 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