padawan_linuxero has asked for the wisdom of the Perl Monks concerning the following question:

Hi
I have a file that I use that is CSV but it has this
caracter at the beginning and end of every word
so I need to get rid of them
I remember that in *nix we had SED
which it was a timesaver but in winblow I
dont have that option so I try to do a script like this
#use strict; my $file = "g:/dbf2csv/reg501.csv"; # Slurp File Contents open(FILE, $file) or die "Can't open $file: $!"; #my $data = do {local $/; <FILE>}; my $newdeli = do {~s/\"//g; <FILE>}; close(FILE);
but did not work

what can i do to fix it?

TIA

Replies are listed 'Best First'.
Re: Delete de " from my file
by FunkyMonk (Chancellor) on Oct 12, 2007 at 18:47 UTC
    Commenting, or removing, use strict doesn't make errors go away, it just makes perl hide the errors from you, so don't do it.

    my $data = do {local $/; <FILE>}; $data =~ s/"//g;

    will remove all the quotes from the slurped text. If you want to remove just the quotes at the start and end of words, try using Text::CSV and post back if you have any problems.

Re: Delete de " from my file
by shmem (Chancellor) on Oct 12, 2007 at 18:56 UTC
    my $newdeli = do {~s/\"//g; <FILE>}

    What does that do?

    1. it applies a substitution to $_ (probably unset currently) and does a bitwise negation on its result (with the ~ operator)
    2. then it reads a line from the filehandle FILE
    3. then it assigns the result from the read operation to $newdeli.

    That's not what you want. Probably you want to read from the filehandle FILE in slurp mode, store the stuff read, and apply a substitution on that:

    my $newdeli = do { local $/; <FILE>} $newdeli =~ s/\"//g;

    or shortcut

    (my $newdeli = do { local $/; <FILE>}) =~ s/\"//g;

    But then, there are modules for reading csv files. Have a look at Text::CSV.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: Delete de " from my file
by Aim9b (Monk) on Oct 12, 2007 at 19:16 UTC
    I use the following to strip off only the beginning & ending quotes (embedded may valid) on just the DETAIL records, not the Header.
    if ($StRec eq $Dtl) { # For ALL detail records $St =~ s/^\"//; # Remove ONLY the LEADING quote $St =~ s/\"$//; # Remove ONLY the TRAILING quote }
    Hope this helps.