Re: Text file splitter, date/time field



Throw <throw@xxxxxxxxxxxxxx> wrote:

A. Sinan Unur wrote:

"Throw" <throw@xxxxxxxxxxxxxx> wrote in news:1140187205.361247.173780
@g43g2000cwa.googlegroups.com:

except I need to split a concatenated PHP file.


I suspect that what you need done is a great deal different
from the subject of this thread.

The OP has markers only at the beginning of records, you have
them at the beginning and at the end.

The OP's markers are variable length, yours are fixed strings.

The OP's output filenames are derived from what is matched, you
haven't indicated any way of naming the files.


Basically, I have one
large text file into which I have copied PHP file after PHP file, and
now I want to split them up again. The PHP file always begins with
<?php


What have you tried and what has failed?

I have tried the following if-lines and other variations thereof:

if ( \<?php ) {


That is not the syntax for the match operator:

perldoc -f m

then:

perldoc perlop

The match operator starts with either an "m" or a "/" character,
not a "\" character.


if ( /\<\?php ) {


The match operator ends with a "/" character.

If you add that character, then it should match just fine,
though it has one extra backslash that is not needed.


if ( /\<\?\p\h\p ) {
if ( \<?php [AP]M$/ ) {
if ( /\<\?php [AP]M$/ ) {
if ( /\<\?\p\h\p [AP]M$/ ) {
if ( \<?php ) {
if ( /^\<\?php ) {
if ( /^\<\?\p\h\p ) {
if ( \<?php [AP]M$/ ) {
if ( /^\<\?php [AP]M$/ ) {
if ( /^\<\?\p\h\p [AP]M$/ ) {

Does that answer your question?


It answers the underlying unspoken question quite well.

You appear to want to write code in a language that you do not know.

The implication is that you want us to write your code for you.

(most especially since you have asked us to write code for you before.)


The problem, I think it should be
clear, is that I do not understand Perl regex syntax,


Then you go learn about it before you write it.

Trying random things will take much more time than learning
the language that you wish to speak.


and is therefore
forced to resort to brute-force methods.


That is absurd.

If you do not know a language, you go learn the language.

You can learn about the syntax for the m// operator and for
Perl's regular expression in the documentation that came with perl.

If you don't understand some part of those docs, then post a question
about it here and we will help you understand it.

We are not likely to read those docs to you though.


Please read the posting guidelines for this group. They provide you with
invaluable information you can use to help your self as well as helping us
help you.

None of said posting guidelines helps me to help myself


They most certainly do!

- Check the Perl Frequently Asked Questions (FAQ)

Since you have a question about pattern matching, you would
eventually try:

perldoc -q pattern

And would have found:

How can I pull out lines between two patterns that are themselves on
different lines?

Which tells you how to do exactly what you need done!


- Check the other standard Perl docs (*.pod)

Which describe the syntax for the operator that you want to use.


- Use an effective followup style

Wherein you quote what you are commenting on, such as the
code that you want modified.

This helps you because it allows more people to examine the problem.

Many or most readers will just move on to answering the next person's
question rather than spend time locating the code.


nor does it
help you any more to help me than my initial post already does...


- Provide enough information

(which asks for a short and complete program that we can run
that illustrates the problem you need solved.)

If you posted code missing the match operator's closing slash,
then we could have told you that were missing the closing slash,
and one of your problems would have been eliminated straightaway
rather than here way down-thread.

Are the "<?php" and "?>" always on separate lines?

If you had posted data to go with your code, we would have been able
to see that there was a much better way of solving your problem
than what appeared in the thread thus far.


Anyway, here is a short and complete program that *you* can run.

----------------------------------------
#!/usr/bin/perl
use warnings;
use strict;

my $cnt=1;
while ( <DATA> ) {
if ( /<\?php/ ) {
open OUT, '>', "$cnt.php" or die "could not open '$cnt.php' $!";
$cnt++;
}
print OUT if /<\?php/ .. /\?>/;
}

__DATA__
extra stuff
<?php
1st PHP section
?>
in-between stuff
<?php
2nd PHP section
?>
trailing stuff
----------------------------------------


don't
you agree?


No.

You have already used up all of your coupons.

So long!


--
Tad McClellan SGML consulting
tadmc@xxxxxxxxxxxxxx Perl programming
Fort Worth, Texas
.



Relevant Pages