LinuxQuestions.org
Review your favorite Linux distribution.
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 04-02-2007, 09:33 AM   #1
rjcrews
Member
 
Registered: Apr 2004
Distribution: Debian
Posts: 193

Rep: Reputation: 30
Perl replace backslash


Hi all,

Any ideas why this code will not open the file and replace the backslashes? Trying to do a .csv import into a DB and the \, is screwing it up if it occurs in the file. I have tried about 10 different ways to remove the \ from the data file.

If this is the correct way to remove backslashes, am I not rewriting the file correctly?

Thanks!

Code:
open UPLOADFILE, ">$upload_dir/$filename";

binmode UPLOADFILE;

while ( <$upload_filehandle> )
{
  print UPLOADFILE;
}

close UPLOADFILE;



open (FILE, "/$upload_dir/$filename");
while(<FILE>){

        $_ =~ s/\\//g;

}
close(FILE);
 
Old 04-02-2007, 10:46 AM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
I think you want instead:

Code:
open UPLOADFILE, ">$upload_dir/$filename";

binmode UPLOADFILE;

while ( <$upload_filehandle> )
{
  $_ =~ s/\\//g;
  
  print UPLOADFILE;
}

close UPLOADFILE;
Different people have different styles of writing Perl code. You seem to prefer extensive use of $_ and avoiding mentioning it explicitly when you do, so maybe you want this equivalent code:

Code:
open UPLOADFILE, ">$upload_dir/$filename";

binmode UPLOADFILE;

while ( <$upload_filehandle> )
{
  s/\\//g;
  
  print UPLOADFILE;
}

close UPLOADFILE;
This code removes the backslashes. But you mentioned replacing them, not removing them. Replacing them with what?

If you indeed do want to replace them with something else, do this:

Code:
open UPLOADFILE, ">$upload_dir/$filename";

binmode UPLOADFILE;

while ( <$upload_filehandle> )
{
  s/\\/somethingelse/g;
  
  print UPLOADFILE;
}

close UPLOADFILE;
Would that something else be a forward slash? If so, don't forget to "escape" it, thus:

Code:
open UPLOADFILE, ">$upload_dir/$filename";

binmode UPLOADFILE;

while ( <$upload_filehandle> )
{
  s/\\/\//g;
  
  print UPLOADFILE;
}

close UPLOADFILE;
Hope this helps.
 
Old 04-02-2007, 12:02 PM   #3
rjcrews
Member
 
Registered: Apr 2004
Distribution: Debian
Posts: 193

Original Poster
Rep: Reputation: 30
That did help, thanks, I did not realize it was that easy to do the replacement while writing the file.
 
  


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
sed command to replace slash with backslash stelmed Slackware 13 08-19-2010 05:01 AM
Perl: replace character in file vippie Programming 4 03-23-2007 04:26 AM
perl: global replace using hash WindozBytes Programming 6 12-10-2006 05:20 PM
How do i change spaces inside a txt file, and replace space with backslash \? repolona Linux - Newbie 1 05-24-2006 05:08 AM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 01:04 AM


All times are GMT -5. The time now is 03:06 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration