in reply to How to remove underscore at the end of the line?

s/_$//

Replies are listed 'Best First'.
Re^2: How to remove underscore at the end of the line?
by Anonymous Monk on Mar 17, 2017 at 04:47 UTC
    Also, the "code you tried" does NOT produce the "output you obtained," so I'm not sure what you're actually doing.
Re^2: How to remove underscore at the end of the line?
by finddata (Sexton) on Mar 17, 2017 at 05:04 UTC
    But it is not displaying the expected output as per you mentioned.

      Then you must be using it wrong. Show us how you used the solution posted.

      I am confident that s/_$// works for the sample data you posted. Show us your code.


      Dave

        Hi finddata
        As suggested by davido Please check below it works,
        #!/usr/local/bin/perl use strict; use warnings; my $str = 'DCMS_DEMO_new_block2_checklist_tmp_rev1_'; chop $str; print "$str\n"; my $regstr = 'DCMS_DEMO_new_block2_checklist_tmp_rev1_'; $regstr =~ s/_$//; print $regstr;

        OUTPUT:
        DCMS_DEMO_new_block2_checklist_tmp_rev1
        DCMS_DEMO_new_block2_checklist_tmp_rev1

        Also for your current input, a chop will also work (which is not recommended if your input line does not have _ always at end.It will remove the last character blindly)
        As other monks suggested, please try first and post the code which is not working instead of expecting the full code by other monks here.
        sub ir($$); sub ir($$) { my $prefix = shift @_; $prefix=~ s/_$//; print $prefix,"\n"; my $dir = shift @_; print "&&&$dir&&&","\n"; opendir(DIR, $dir) or die $!; my @entries = readdir(DIR); print "****@entries***"; close(DIR); foreach my $file (@entries) { next if ($file =~ /^\.+$/); if ( -d $dir . '/' . $file) { ir($prefix . $file .'_', $dir . '/' . $file); } elsif ( ( -f $dir . '/' . $file ) && ( $file =~ /\.config$/ +) && ($file !~ /^$prefix/)) { my $suffix = $file; $suffix=~s{\A[^.]*}{}xms; print "^^^$suffix^^^","\n"; # print $suffix,"\n"; rename $dir . '/' . $file, $dir . '/' . $prefix . $suffix +; } } } ir('',$output_dir); here print prefix has underscore at the end of the line

      Just to drive davido's point home:

      c:\@Work\Perl\monks>perl -wMstrict -le "for my $s (qw( DCMS_DEMO_ DCMS_DEMO_new_block2_ DCMS_DEMO_new_block2_checklist_tmp_ DCMS_DEMO_new_block2_checklist_tmp_rev1_ )) { my $subject = $s; print qq{'$subject'}; $subject =~ s/_(?=[^_]*$)//; print qq{'$subject' \n}; } " 'DCMS_DEMO_' 'DCMS_DEMO' 'DCMS_DEMO_new_block2_' 'DCMS_DEMO_new_block2' 'DCMS_DEMO_new_block2_checklist_tmp_' 'DCMS_DEMO_new_block2_checklist_tmp' 'DCMS_DEMO_new_block2_checklist_tmp_rev1_' 'DCMS_DEMO_new_block2_checklist_tmp_rev1'
      That's with your original regex, and I get the same output with  s/_$// also.


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