Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read file, remove particular section and print the file

Status
Not open for further replies.

greggK

MIS
Aug 21, 2002
1
US
I want to know how can I open up a file, look for specific text in it, and remove it.
For example
You open up a file it contains:

TEXT
TEXT
TEXT

<!-- Remove Start -->
more text to be removed here.
<!-- Remove End -->

TEXT
TEXT
TEXT

<!-- Remove Start -->
more text to be removed here.
<!-- Remove End -->

Once you are done it's only supposed to have

TEXT
TEXT
TEXT

TEXT
TEXT
TEXT

So you would have removed the text or paragraphs between the <!-- Remove Start --> and <!-- Remove End --> and you would also remove the <!-- Remove Start --> and <!-- Remove End -->
And it should be able to remove multiple instances of the text as shown in the example. Is this possible? How easy is it? Can anyone give me an example of how to do this?
 
Hello greggk,
welcome to TTs.

' Pretty straigth forward,
#!/usr/local/bin/perl
# open your input file
open(IPF,&quot;<input_file_name_here&quot;) or die &quot;Failed to open input file, $!\n&quot;;
while (<IPF>) { $buffer .= $_; }
close IPF;

# now all of input file is in $buffer
# use a regex replace to excise the unwanted chunks.
# <!-- Remove Start -->
# more text to be removed here.
# <!-- Remove End -->

$buffer =~ s/<!-- Remove Start -->.*?<!-- Remove End -->//gs;

# open an output file and print shortened version of file
open(OPF,&quot;>some_output_file&quot;) or die &quot;Failed to open OPF, $!.\n&quot;;
print OPF &quot;$buffer&quot;;
close OPF;

'kinda smells like a school assignment. I'm doing someone's homework?, am I?

I hope this helps.




keep the rudder amid ship and beware the odd typo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top