Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to delete quote (") from a text file ? 2

Status
Not open for further replies.

moretips

MIS
Sep 25, 2000
25
US
Hi, I with to delete quote (") in a text file like below :
1243| "ABC" | test |

I tried to use s///, tr /// but failed to do it. Below is my script:

#usr/bin/perl

open(INFILE, &quot;<client.txt&quot;) || die &quot;can't open file;

while (<INFILE> ) {

open(OUTFILE, &quot;>client.txt&quot;) || die &quot;can't open file;

tr/&quot;//d;
}

close (INFILE);
close (OUTFILE);

---------------------------------------------

Above script delete all the client.txt, may i know what's wrong with my script ?

Thanks for ur help !!!!!!

 
Hello Kenix,
A few comments.....
You are repeatedly repeatedly openning your OUTFILE inside your while loop. That will cause some real confusion. I think you probably want to open the OUTPUTFILE before the loop, print to it inside the loop, and close it after the looping is done.
I am in the habit of using s///, so, this works.....

#!/usr/local/bin/perl

# open a file to write to.
open(OUTPUTFILE,&quot;>someOtherFile&quot;) or die &quot;Failed to open OUTPUTFILE, $!\n&quot;;

# read input into a buffer variable
open(INPUTFILE,&quot;<someFile&quot;) or die &quot;Failed to open input file, $!\n&quot;;
while ($line = <INPUTFILE>)
{
$line =~ s/&quot;//g;
print OUTPUTFILE &quot;$line&quot;;
}
close INPUTFILE;
close OUTPUTFILE;

This is a little verbose for clarity.
'hope this helps.



keep the rudder amid ship and beware the odd typo
 
Instead of using the while loop, I think you could shorten it like this:

open(OUTPUTFILE,&quot;>someOtherFile&quot;) or die &quot;$!&quot;;
open(INPUTFILE,&quot;<someFile&quot;) or die &quot;$!&quot;;

$input = join(&quot;\n&quot;,<INPUTFILE>);
$input =~ s/&quot;//g;
print OUTPUTFILE &quot;$input&quot;;

close INPUTFILE;
close OUTPUTFILE;

Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Thanks for both of u !!!!

i tried to use above 2 method, the result still the same, that's .. my client.txt become an empty file.

Is it b'cos of the &quot;|&quot; between the string ? or some other reason.

Regards,
Kenix
 
hi !!!! sorry it's my mistake.....
actually above 2 method is working. B'cos of i use the same file name, it overwrite the file. After i used different file names for these 2 .txt, it is working !!!

goBoating n tanderso, Thank you very much !!!!!!!
 
This is an old post, but the solution doesn't work for me. The line that I've added is:
$from =~ s/&quot;//g;

Below are all the errors this line generates. I've tried escaping the double-quote to no avail. Any ideas?
-Mike

<stdin>:57:21: warning: multi-line string literals are deprecated
<stdin>:59:44: warning: multi-line string literals are deprecated
<stdin>:69:41: warning: multi-line string literals are deprecated
<stdin>:79:15: warning: multi-line string literals are deprecated
<stdin>:80:15: warning: multi-line string literals are deprecated
<stdin>:83:85: warning: multi-line string literals are deprecated
<stdin>:83:85: missing terminating &quot; character
<stdin>:57:21: possible start of unterminated string literal
Can't find string terminator '&quot;' anywhere before EOF at ./processDir.pl line 98.
 
What are the few lines above (and maybe below) the one you added? It's more likely you have an unmatched quote or left off a semicolon somewhere up there.

----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
icrf,

Way too polite.

It worked 2 years ago, whose fault is it now??

--Paul

 
Well, all I know is that when I comment out this line, all is well. Uncomment it, and I get the spew of errors. I now agree that the root of the problem is neighbouring code, but WHY is the neighbouring code OK without this line??

Ahhhh.... such is Perl. Doesn't matter anymore now. I've reworked this routine to not need quote removal.
-thanks anyway.

p.s. whose fault?? Obviously Larry's! (just joking -please don't flame me!! ;-{)
 
this script here

$str = 'lk&quot;';

$str =~ s/&quot;//;

print &quot;$str\n&quot;;

seems to work -- I suspect that you have other problems with your script that's confusing Perl, have a look for an unmatched double quote before that line in your script

Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top