Re: Trim Two Characters On Line
by BillKSmith (Monsignor) on Oct 23, 2020 at 03:34 UTC
|
Here is exactly what you request.
substr( $line, -2, 2, '' ) if $line =~ /sum/;
You may want to remove the second and third last character, leaving a newline at the end.
substr( $line, -3, 2, '' ) if $line =~ /sum/;
| [reply] [d/l] [select] |
|
Thank you Bill. This was the perfect one liner for what I was looking for, but I ended up running into another problem, after running the process on all my files, come to find out, not all of them have the extra commas at the end as I expected. Would it be okay to send you a message and show you what I am trying to do.
| [reply] |
|
not all of them have the extra commas at the end
Are you trying to read a CSV file? Better use Text::CSV (and its accelerator Text::CSV_XS), it can handle all of the evil edge cases that you didn't even think of.
Would it be okay to send you a message and show you what I am trying to do.
Perlmonks does not work that way. Post detail questions right here in this thread.
Alexander
--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
| [reply] |
|
|
|
|
|
|
Re: Trim Two Characters On Line
by johngg (Canon) on Oct 22, 2020 at 23:14 UTC
|
You can use chop to remove a character from the end of a string, it works on $_ by default. I assume that you don't want to get rid of the line terminator so I remove it first using chomp then it gets replaced by say when the line is printed.
johngg@abouriou:~/perl/Monks$ perl -Mstrict -Mwarnings -E '
open my $inFH, q{<}, \ <<__EOD__ or die $!;
this line does not match
this SUM is wrong
__EOD__
say q{};
while ( <$inFH> )
{
chomp;
do {
say;
next;
} unless m{SUM};
chop; chop; say;
}
close $inFH or die $!;'
this line does not match
this SUM is wro
I hope this is helpful.
| [reply] [d/l] [select] |
Re: Trim Two Characters On Line
by hippo (Bishop) on Oct 23, 2020 at 08:16 UTC
|
I have read many forums
Maybe just the documentation would have been enough. Since you can write pseudocode, we'll assume you are a programmer and are familiar with the usual constructs.
For if ... then ... else ... end if see Compound Statements.
For LINE contains "SUM" see index or perlre.
For remove last two characters in line you have lots of options, including substr, chop, the s/// operator, etc.
Note that there are many Tutorials right here in the Monastery to help you get started. Enjoy.
| [reply] [d/l] [select] |
Re: Trim Two Characters On Line
by tybalt89 (Monsignor) on Oct 23, 2020 at 13:21 UTC
|
perl -le 'for(@ARGV){ s/(?=SUM).*\K..//; print }' abcSUMcdexx abcSUXcd
+exx abcSUMc
Outputs:
abcSUMcde
abcSUXcdexx
abcSU
( borrowing bits and pieces and ideas from other monks... )
| [reply] [d/l] [select] |
Re: Trim Two Characters On Line
by LanX (Saint) on Oct 22, 2020 at 23:23 UTC
|
$line =~ s/(SUM.*)(..)/$1/
should meet your requirement.
Bonus: $2 will hold the truncated characters.
Demo in the debugger
DB<1> $_ = 'abcSUMcdexx'
DB<2> s/(SUM.*)(..)/$1/
DB<3> p
abcSUMcde
DB<3> $_ = 'abcSUXcdexx'
DB<4> s/(SUM.*)(..)/$1/
DB<5> p
abcSUXcdexx
DB<5>
Update: typo fixed in DB<1> , thx to AnomalousMonk for spotting.
| [reply] [d/l] [select] |
|
DB<1> $_ = 'abcSUXcdexx'
DB<2> s/(SUM.*)(..)/$1/
DB<3> p
abcSUMcde
comes about. A typo?
Give a man a fish: <%-{-{-{-<
| [reply] [d/l] [select] |
|
Oh, I get your point. But that's how I understood the question.
The OP should clarify.
But you're right the pseudo code says otherwise.
> A typo?
Kind of, a side effect of the default page editing/cursor mode when I use the perldb inside emacs.
Thanks fixed! :)
| [reply] |