Reputation: 47
I have this sample data I wanted to remove the comma particularly the last comma of the line, btw it's .csv file.
W, S, Vbd2OX/NW, Vbd2OX/PW, Vbd3OX/NW, Vbd3OX/PW, ID,ID, 11200, V 11200, V 11200, V 11200, 6,1,0.69,5.56,-5.56,10.5,-10.12, 6,2,3.99,7,-5.13,-7,640.455,
this 2 code is not working the last comma is still there
$line =~ s/\,$//g;
and
$line =~ s/,//g;
Upvotes: 1
Views: 184
Reputation: 6798
Or other variation of code
use strict;
use warnings;
while( my $line = <DATA> ) {
$line =~ s/\,\s*$//;
print "$line\n";
}
__DATA__
W, S, Vbd2OX/NW, Vbd2OX/PW, Vbd3OX/NW, Vbd3OX/PW,
ID,ID, 11200, V 11200, V 11200, V 11200,
6,1,0.69,5.56,-5.56,10.5,-10.12,
6,2,3.99,7,-5.13,-7,640.455,
Upvotes: 1
Reputation: 6798
Following code works for provided set of data
use strict;
use warnings;
while( <DATA> ) {
/(.+)\,\s*/;
print "$1\n";
}
__DATA__
W, S, Vbd2OX/NW, Vbd2OX/PW, Vbd3OX/NW, Vbd3OX/PW,
ID,ID, 11200, V 11200, V 11200, V 11200,
6,1,0.69,5.56,-5.56,10.5,-10.12,
6,2,3.99,7,-5.13,-7,640.455,
Upvotes: 0
Reputation: 98398
If there are spaces after the last comma (which seems to be true for a couple of your lines), you need to:
s/, *$//;
No need to backslash-escape a comma; no need for /g
when you want the substitution to happen at most once per line.
Upvotes: 6