Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

script for removing certain text

by Anonymous Monk
on Jun 29, 2001 at 18:46 UTC ( [id://92646]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks, I would like to remove certain section of text from a file specified by begin and end keyswords. Does anyone of you have a perl script which could do this (given begin and end word). Thanks very much J.

Replies are listed 'Best First'.
Re: script for removing certain text
by thpfft (Chaplain) on Jun 29, 2001 at 19:29 UTC

    The good news is that perl is exactly the right way to do what you describe, and the task you describe is a good way to start learning perl.

    The bad news: even if people were inclined to just provide the solution to your problem, without the very small amount of perl knowledge you would need to start solving it yourself, the chances of applying someone else's solution successfully are very slim.

    So you'll have to do a bit more work, i'm afraid. but we can start things off. the sequence of events you need, at its simplest:

  • read a file into memory
  • remove whatever falls between your markers
  • write what remains of the file back to disk
  • So you need to learn about file input and output. The s/// operator will probably be the way to remove those parts you don't want, and you'll need to know a little about string matching and regular expressions to make that work. Then you'll just print what remains back to the same file (or another one).

    If you need to learn about the basics of perl and the structure of a simple script, you really need the camel book. Failing that, there are tutorials here and elsewhere.

Re: script for removing certain text
by dooberwah (Pilgrim) on Jun 29, 2001 at 19:00 UTC
    If I understand your question correctly a regular-expression something like this would work:

    s/hello.*goodbye//g;

    Where the begin word is "hello" and the end word is "goodbye". If you in *NIX (this might work in Windoze also but I havn't tested it) you could give this as a command line argument using the -p -i and -e switchs:

    perl -pi -e 's/hello.*goodbye//g' [FILENAME]

    -Ben Jacobs (dooberwah)
    one thing i can tell you is you got to be free
    http://dooberwah.perlmonk.org

Re: script for removing certain text (boo)
by boo_radley (Parson) on Jun 29, 2001 at 19:02 UTC
    perlmonks is more oriented to technical discussion of perl, and not entirely as a trading post for code samples or entire scripts... You may find something useful in snippets or craft, but I'd recommend going to a site geared towards being a repository, such as the perl archive. Good luck.
Re: script for removing certain text
by bikeNomad (Priest) on Jun 29, 2001 at 19:03 UTC
    If you want to remove the beginning and end lines as well, you can do this as a one-liner, editing the fields in place:

    perl -nie "print unless /begin/../end/" file1 file2

    Of course, the details of quoting, etc. are up to your shell (like for instance, you'd use " instead of ' under Windows shells).

    If you want backup files to be made, do this:

    perl -ni.bak -e "print unless /begin/../end/" file1 file2

    This will make backup files that are named file1.bak and so on.

      Hi Nomad, I tried your command, but it says "Can't open perl script "print unless /specify/../endspecify/": No such file or directory"..... Any guess. Thanks J
Re: script for removing certain text
by thraxil (Prior) on Jun 29, 2001 at 19:04 UTC
    # $text is the main text you want to process # $begin is the begin keyword # $end is the end keyword $text =~ s/\Q$begin\E.*\Q$end\E//s;

    the '\Q' and '\E' around '$begin' and '$end' quote them so that even if they contain regexp characters, they won't do unexpected things. see the perlre manpage. of course, i'm sure someone will point out that dotstar is bad, but the question was pretty vague.

    anders pearson

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-04-25 15:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found