G'day novice2015,

TMTOWTDI:

#!/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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.