remove specific data from a line
Anonymous Monk
created: 2006-02-02 10:07:43
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=) {
     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
Re: remove specific data from a line
created: 2006-02-02 10:20:30
So you wanna remove the string if it start with myword? Try this:
if $line =~ /^myword.*$/ {
     $line =~ s/$&//g
    }
Re: remove specific data from a line
created: 2006-02-02 10:26:29
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
    }
Re^2: remove specific data from a line
created: 2006-02-02 10:36:50
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
Re^3: remove specific data from a line
created: 2006-02-02 22:17:22
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 content
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 everything 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' ));
Re^4: remove specific data from a line
created: 2006-02-03 05:06:38
Thank for answer but not quite what I look for. This s/abc\w+\s+\w+(?:, *\w+)\s+//; only work correct on last line of my example.
I'm not able to use second or third option you supply because I do not know what end of line hold only what beginning of line look like. See my reponse to Roy Johnson. Thanks
Re: remove specific data from a line
created: 2006-02-02 10:36:24
I think you want:
$line =~ s/^myword \S+//; # strip myword, a space, and any stretch of non-spaces

Caution: Contents may have been coded under pressure.
Re^2: remove specific data from a line
created: 2006-02-03 04:54:38
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
Re^3: remove specific data from a line
created: 2006-02-03 13:29:04
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.

perlmonks.org content © perlmonks.org and Anonymous Monk, graff, idle, Roy Johnson

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03