G'day novice2015,
#!/usr/bin/env perl use strict; use warnings; while (my $init_line = <DATA>) { my $neg_class_line = $init_line; my $neg_assert_line = $init_line; $neg_class_line =~ s/,([^,]*)$/$1/; $neg_assert_line =~ s/,(?!.*,)//; print 'Initial line: ', $init_line; print 'Negated class: ', $neg_class_line; print 'Negative assertion: ', $neg_assert_line; print '-' x 40, "\n"; } __DATA__ 21112,/vol/voly,blx 21113,/vol/eng,blz 21114,/vol/eng,file
Output:
Initial line: 21112,/vol/voly,blx Negated class: 21112,/vol/volyblx Negative assertion: 21112,/vol/volyblx ---------------------------------------- Initial line: 21113,/vol/eng,blz Negated class: 21113,/vol/engblz Negative assertion: 21113,/vol/engblz ---------------------------------------- Initial line: 21114,/vol/eng,file Negated class: 21114,/vol/engfile Negative assertion: 21114,/vol/engfile ----------------------------------------
See perlre for a description of these, and other, methods.
Use Benchmark to find the most efficient method (if that's important to you).
And finally, a quick, friendly note on being specific. In one place you say "last comma" and in another you say "second comma": in the current (or future) data, these may not be the same. My two examples operate on the last comma (or change nothing if no commas are present). Adding these additional DATA lines:
W,X,Y,Z W,X,YZ W,XYZ WXYZ
I get this additional output:
Initial line: W,X,Y,Z Negated class: W,X,YZ Negative assertion: W,X,YZ ---------------------------------------- Initial line: W,X,YZ Negated class: W,XYZ Negative assertion: W,XYZ ---------------------------------------- Initial line: W,XYZ Negated class: WXYZ Negative assertion: WXYZ ---------------------------------------- Initial line: WXYZ Negated class: WXYZ Negative assertion: WXYZ ----------------------------------------
— Ken
In reply to Re^3: sed command in perl
by kcott
in thread sed command in perl
by novice2015
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |