Reputation: 309
I have a Line that is pipe delimited:
John |DEMME|"9 Snowy "" Court"|WERRIBEE|""VIC""
I split my line to each fields
@fields = split (/\|/, $_);
what I want is to remove the double quotes in the beginning/end of each fields but it should retain the double quotes that are in between.
expected output
John |DEMME|9 Snowy "" Court|WERRIBEE|VIC
I also tried this
s/^\"|"$//g;
but what it does is it reads by line not but fields, so it will only remove the double qoutes which are at the beginnig and end of the line.
another scenario:
John |DEMME| "Shop 6A ""Atlantic on Coolum""|WERRIBEE|VIC
output should be
John |DEMME| Shop 6A "Pacific on Coolum"|WERRIBEE|VIC
I hope you guys can help me with this.
thank you very much
Upvotes: 1
Views: 3202
Reputation: 2264
try this one dude
my $_='John |DEMME|"9 Snowy "" Court"|WERRIBEE|""VIC""';
my @fields = split (/\|/, $_);
foreach my $item(@fields){
$item=~s/^\"+//g;
$item=~s/\"+$//g;
print "$item";
}
Upvotes: 1
Reputation: 196
The quotes could also be removed from each field using a map
expression:
@fields = map { s/^"(.*)"$/$1/; $_ } split (/\|/, $_);
split
breaks the line apart into a list, while the map
applies the substitution to each member of the list. To join them back up:
print join('|', @fields), "\n";
Upvotes: 0
Reputation: 183251
If it's reading by lines, then you could write:
s/(?:^|(?<=\|))"|"(?=$|\|)//g;
to remove "
at the start of a line or after a |
, or at the end of a line or before a |
.
(The (?<=...)
notation creates a zero-width positive "lookbehind" assertion, which in this case checks to see if there's a |
preceding; the (?=...)
notation creates a zero-width positive "lookahead" assertion, which in this case checks to see if there's a |
or end-of-file following.)
Upvotes: 2