Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Deleting everything before a string

by Anonymous Monk
on Dec 21, 2000 at 06:52 UTC ( [id://47701]=perlquestion: print w/replies, xml ) Need Help??

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

I've got a quick question:

I'm opening up a file, giving everything in the file to the variable $content, and now what I want to do is delete everything before and after the instance of a string. For example, if my file contained:

blahblahblahblahblahblah+_john+_blahblahblahblahblahblah, I want to go through the file and substitute everything before and after the +_ with white space, so that $content would only consist of "john".

Is this something I can do with substitute?

Replies are listed 'Best First'.
Re: Deleting everything before a string
by I0 (Priest) on Dec 21, 2000 at 07:04 UTC
    $content =~ s/.*\+_(.*?)\+_.*/$1/s; #or $content = (split/\+_/,$content)[1];
      I would prefer
      @content = ($content =~ /\+_(.*?)\+_/sg);
      since it would work for more that occurance of the replacement string in the file.

      /\/\averick

        Although it is unclear to me how Anonymous Monk would want to handle in that case. Perhaps Anonymous Monk would prefer @content = ($content =~ /\+_(.*?)(?=\+_)/)sg); or /\+_(.*)\+_/s
      And then of course writing the new $content over the file you read in from.

      On the other hand if you are dealing with a large file then I would suggest searching line by line (or chunk by chuck) for +_...+_ and then storing that, closing the file, writing the found information to a new file of the same or different name.

        Or perhaps setting $/ to '+_' and $^I to '.bak'
Re: Deleting everything before a string
by fundflow (Chaplain) on Dec 21, 2000 at 08:06 UTC
    Instead of adding extra stuff and then erasing it, you could just as well do:
    while (<MYFILE> ) { $content .= $_ if /start-string/ .. /end-string/ }
Re: Deleting everything before a string
by cat2014 (Monk) on Dec 21, 2000 at 09:28 UTC
    the easiest way is just s/^(.*)\+_/\s\+_$1\+_/s
    of course, there are a few caveats with that:
    i don't believe that . matches the new line character, (unless you have the s at the end of the s///) and you will only end up with one space for all the stuff before the + that you replaced. did you want just one white space character, or 1 for every character that you replaced? and i just realized that you want to delete after the +, too. the first answer is much better than my quick regex. would tr/// work for this?
Re: Deleting everything before a string
by Cybercosis (Monk) on Dec 21, 2000 at 15:05 UTC
    Okay, this is going to be evil. (note to self: don't program in Perl after learning Standard ML...) but it (might) be somewhat faster, since there's no regex stuff:
    my $first_index = index($content, "+_"); my $second_index = index($con +tent, "+_", $first_index); $content = substr($first_index, $first_index - $second_index);
    Okay, that wasn't so bad... my Perl instincts almost raged against this, which may not even work:
    $content = substr($content, index($content, "+_")+2, index($content, " ++_") - index($content, "+_", index($content, "+_")+1)-4);
    *shudder* damn functional languages... =-)
Re: Deleting everything before a string
by snax (Hermit) on Dec 21, 2000 at 14:59 UTC
    From the description of your algorithm, it sounds like you want $content to contain only a specific string if that string is contained within the file in question, right? Or do you really mean you want to replace everything else with whitespace, so that xxxJOHNxxx would be three spaces, JOHN, and then three spaces?

    In the first case, how about

    ($file_text =~ /$string_to_match/) && $content = $string_to_match;
    There's no need to do a substitution since you already know what you want the value of $content to be. Moreover, you probably have a default value in mind if the file fails to contain the match string, in which case you could use the ternary operator to do it:
    $content = ($file_text =~ /$string_to_match/) ? $string_to_match : $default_failure_value;
    Finally, if you know the file contains the text, and that's what you want in the $content variable, just do a simple assignment.

    Update:
    In case it's not obvious, I construed the question to imply that the matching string was known beforehand, rather than the delimiters. If the delimiters are what is known beforehand, my advice is, well, pointless.

Log In?
Username:
Password:

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

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

    No recent polls found