Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

how to remove a string from end of a line

by rpinnam (Novice)
on Oct 09, 2015 at 18:58 UTC ( [id://1144327]=perlquestion: print w/replies, xml ) Need Help??

rpinnam has asked for the wisdom of the Perl Monks concerning the following question:

My input :

RcdA|CON|139|||Kan|13|J|J|607|abc@gmail.com

I want my output as:

RcdA|CON|139|||Kan|13|J|J|607|

  • Comment on how to remove a string from end of a line

Replies are listed 'Best First'.
Re: how to remove a string from end of a line
by graff (Chancellor) on Oct 09, 2015 at 22:10 UTC
    The information you provided isn't much to go on. I'm sure you have more than just the one example string to deal with, so the question is: in what ways are all the other strings similar and not similar to the one example you've given?

    Any of the following might serve as the strategy that solves not only the one example you gave, but all the other cases:

    • Delete all characters after the last "|" character in the string
    • Delete all characters from the 10th "|" character to the end of the line
    • Delete all characters after the 10th "|" character, but before the 11th one (if there is an 11th one)
    • Delete any contiguous string of non-"|" characters that happens to resemble an email address (or contain "@")

    The point is that the solution depends on what you know about the input data - and how consistent the input is and what kinds of consistency it has (or doesn't have) - as well as how the output should differ.

    I tend to do a lot of "defensive programming" whenever it comes to applying this sort of filter to text data - a lot of data diagnosis (where possible) before writing the code, and enough tests in the code to make sure that the filter does what it's supposed to do, and reports when the input doesn't meet the necessary expectations.

Re: how to remove a string from end of a line
by flexvault (Monsignor) on Oct 09, 2015 at 22:02 UTC

    rpinnam,

    Use 'rindex' and 'substr' to get your results.

    Regards...Ed

    "Well done is better than well said." - Benjamin Franklin

Re: how to remove a string from end of a line
by stevieb (Canon) on Oct 09, 2015 at 19:13 UTC

    Have you tried anything yet? I know that you're new here, so I'll point out that you should always post the code that you've tried or at least state what you've researched to try to solve the problem yourself. Often, you won't get a response at all and you'll take some flack by not showing that you've made some sort of effort yourself.

    Here is one way to remove what you want removed, assuming that what is being removed will always be in the same position:

    use warnings; use strict; my $str = 'RcdA|CON|139|||Kan|13|J|J|607|abc@gmail.com'; $str =~ s/(.*)\|.*/$1/; print "$str\n";

      Rather than using a capture I think it might be simpler, since the OP mentions "end of a line" specifically, to just remove any non-pipe symbols anchored to the end of the string.

      $ perl -Mstrict -Mwarnings -E ' my $str = q{RcdA|CON|139|||Kan|13|J|J|607|abc@gmail.com}; $str =~s{[^|]*$}{}; say $str;' RcdA|CON|139|||Kan|13|J|J|607| $

      Cheers,

      JohnGG

      I am very new to perl. I am aware of "s///g". But I dont understand what you did here. Can you explain this

       $str =~ s/(.*)\|.*/$1/;

      If I replace $1 with anything(suppose Perl) It's printing only perl. Say about $1 clearly. Thank you

        $str =~ s/(.*)\|.*/$1/;

        1. (.*) Matches and captures all characters, including  | (pipe) (but excluding, in this case, newlines), from the beginning of the string up to, but not including, the right-most pipe. The characters captured are stored in the  $1 regex special variable (see perlvar).
        2. \|.* Matches (but does not capture) all characters from (and including) the right-most pipe to the end of the string.
        3. Effectively, the entire string has now been matched.
        4. /$1/ The replacement portion of the substitution: whatever has been matched is then replaced by whatever was captured in the  $1 capture variable.

        Another way to look at this simple (i.e., up to Perl version 5.6, inclusive) match:

        c:\@Work\Perl\monks>perl -wMstrict -le "use YAPE::Regex::Explain; ;; print YAPE::Regex::Explain->new(qr/(.*)\|.*/)->explain; " The regular expression: (?-imsx:(.*)\|.*) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- ( group and capture to \1: ---------------------------------------------------------------------- .* any character except \n (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ) end of \1 ---------------------------------------------------------------------- \| '|' ---------------------------------------------------------------------- .* any character except \n (0 or more times (matching the most amount possible)) ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------
        See YAPE::Regex::Explain. (Note: This module is good only for version 5.6 and earlier regexes.) See also perlre, perlretut, and perlrequick.

        Update: The regex discussed above will remove the right-most pipe character, but your OPed examples suggest you want to keep this character. If this is so, I would recommend the substitution posted by johngg here.


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1144327]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-25 08:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found