in reply to Re: sed command in perl
in thread sed command in perl

What I want to do is remove the last comma in the file: for example:
21112,/vol/voly,blx 21113,/vol/eng,blz 21114,/vol/eng,file
I want the second comma to be gone. It is not at the end of the line.

Replies are listed 'Best First'.
Re^3: sed command in perl
by AnomalousMonk (Archbishop) on Jun 08, 2016 at 23:03 UTC

    If you have Perl version 5.10 or later, this also works:

    c:\@Work\Perl\monks>perl -wMstrict -le "use 5.010; ;; for my $s ('21112,/vol/voly,blx', '21113,/vol/eng,blz', '21114,/vol +/eng,file') { my $t = $s; $t =~ s{ .* \K , }{}xms; print qq{'$s' -> '$t'}; } " '21112,/vol/voly,blx' -> '21112,/vol/volyblx' '21113,/vol/eng,blz' -> '21113,/vol/engblz' '21114,/vol/eng,file' -> '21114,/vol/engfile'

    Update: Slightly changed code example to enhance clarity.


    Give a man a fish:  <%-{-{-{-<

Re^3: sed command in perl
by kcott (Archbishop) on Jun 09, 2016 at 05:41 UTC

    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

Re^3: sed command in perl
by stevieb (Canon) on Jun 08, 2016 at 22:43 UTC

    Gotcha... then this will look really similar. Just replace the whole sed line with: s/(.*),/$1/;