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

Hello, I need help with Perl problem that I'm having. I just start with Perl and don't have enough knowledge to fix problem quick.
I have file with lines in that look like this
myword myobject,option2,option3 word word word
The words delimited by comma after myobject are optional. There may or may not be these optional words in each line.
myword if present always is at begin of line and if there would always be follow by at least one space then another word (and then possible optional words).
I find the lines in the file that begin with myword like this
while (my $line=<DATA>) { if $line =~ /^myword\s+/ { work on line; } }
I want to strip "myword myobject,option1,option2" from line but not know how do this e.g. something like
if $line =~ /^myword\s+/ { $line =~ s/^myword myobject,options//g }
I knows what first few characters of myword will be but not what myobject will be (except it's always a word).
How does I do this ? ciao

Replies are listed 'Best First'.
Re: remove specific data from a line
by Roy Johnson (Monsignor) on Feb 02, 2006 at 15:36 UTC
    I think you want:
    $line =~ s/^myword \S+//; # strip myword, a space, and any stretch of +non-spaces
    Update: After seeing your clarification, I see that you could have spaces in the comma-separated list. So you'd want:
    $line =~ s/^ #strip, from the beginning of the string, myword\s+ #myword, followed by whitespace \S+ #followed by non-whitespace (?:,\s*\S+)* #followed by any number of comma, optional w +hitespace, non-whitespace //x; # update: two slashes are needed here

    Caution: Contents may have been coded under pressure.
      This one nearly work but not handle this line
      abcefgh qwerty, asdfg yuio jklh
      It print out
      asdfg yuio jklh
      I had to add extra slash to your code like this
      $line =~ s/^abc\w+\s+\S+(?:,\s*\S+)*//x;
      otherwise got errors. I hope this was what you mean. I try to change your code to handle last line which don't leave just  yuio jklh.
      How do I fix this ?
      Thank you for answer so far , it show me how much flexible Perl is
        I forgot to substitute the first word back in.
        $line =~ s/^(abc)\w+\s+\S+(?:,\s*\S+)*/$1/x;
        should give you what you want. You were right about the missing slash.

        Caution: Contents may have been coded under pressure.
Re: remove specific data from a line
by idle (Friar) on Feb 02, 2006 at 15:26 UTC
    So you wanna remove the string if it start with myword? Try this:
    if $line =~ /^myword.*$/ { $line =~ s/$&//g }
    Updated. Oh I see you need to remove the line until the last option. Heres the code:
    if $line =~ /(^myword.*option\d+\s+).*$/ { $line =~ s/$1//g }
      I not explain too good sorry. Let me try eloborate. String could look like this
      abc123 qwerty,asdfg,zxcvb yuio jklh
      or like this
      abc987 qwerty yuio jklh
      or this
      abcefgh qwerty, asdfg yuio jklh
      So I want to end up with same string from all examples above. It should end up like this
      yuio jklh
      Hope this make it bit clearer, sorry for confuse my english not so good
        Given those examples, there might be a few ways to do this -- either try to match from the start of the string:
        s/abc\w+\s+\w+(?:, *\w+)\s+//; # match and remove unwanted initial co +ntent
        or else just look for what you want to keep at the end:
        s/.*\s(\w+\s+\w+)$/$1/; # match desired end content and remove everyt +hing before it
        And of course, if you know in advance how those last two tokens are spelled, you could even use rindex() and substr():
        $_ = substr( $_, rindex( $_, 'yuio jklh' ));