LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


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

Rep: Reputation: 15
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

Last edited by farmerjoe; 01-02-2005 at 02:47 PM.
 
Old 01-02-2005, 03:52 PM   #2
ttolst
Member
 
Registered: Jan 2005
Location: Århus, Denmark
Distribution: Debian GNU/Linux
Posts: 34

Rep: Reputation: 15
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
 
Old 01-02-2005, 04:07 PM   #3
farmerjoe
Member
 
Registered: Oct 2004
Location: Texas
Distribution: Ubuntu - Home, RHEL4 - Server
Posts: 96

Original Poster
Rep: Reputation: 15
Thanks! that should do the trick! i was having a lot trouble changing the ' character using sed alone. perl worked nicely.

-farmerjoe
 
  


Reply


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 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 04:22 PM
Help! Script or commanded needed to replace text in a file farmerjoe Programming 3 01-02-2005 06:59 PM
Script to search and replace in text file - kinda... jeffreybluml Programming 45 11-07-2004 06:37 PM
Search and replace text in file using shell script? matthurne Linux - Software 2 11-02-2004 11:11 AM
Script file to replace large text blocks in files? stodge Linux - Software 0 09-27-2003 11:53 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:31 AM.

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