Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Substitution don't work with a special inputfile

by Laurent_R (Canon)
on Aug 24, 2015 at 14:22 UTC ( [id://1139688]=note: print w/replies, xml ) Need Help??


in reply to Substitution don't work with a special inputfile

Do you want to remove everything in your input lines that comes after [%tqu.*] (including [%tqu)? Please tell your desired result or show us part of the sample output that fir your needs.

But if you want to remove everything from [%tqu to the end of the line, that will not work the same way with your second data sample that has additional line breaks, because data removed will go until the next line break.

Replies are listed 'Best First'.
Re^2: Substitution don't work with a special inputfile
by OldChamp (Acolyte) on Aug 24, 2015 at 16:00 UTC

    Hi Laurent, thank you for your help. I wish to delete everything between the two starting brackets and the two ending brackets including these brackests, that is every string with this pattern

    {[%tqu "What is White's next move?","","",g3,"",0,b6,"misses the win:",0]}

    should be deleted. I have modified the code like Eily suggested and I have added the /s modifier to catch the line breaks, but it still doesn't work, the outputfile is the same as the inputfile. My real data file is far bigger than the second example file, but I think if the code works with this inputfile it will work with the original file too.

      Not sure if this is what you want as it also removes the newlines and re-inserts them before the 1. 2. etc

      #!perl # Aufruf: perl removeEK1.pl TestEK.txt > Out.txt use strict; use warnings; my $regex = '{\[%tqu[^\]]*]}'; my $line = do { local $/; <>; }; $line =~ s/\n/ /g; $line =~ s/$regex//gi; $line =~ s/ (\d+\. )/\n$1/g; print $line;
      poj

        Hi poj, thank you so much for your help, this is the first solution that does the job, but only, when the newlines were also re-inserted in the so called Header or tags, that are the strings starting with a square bracket. As you might have supposed, its a special text file with chess moves, but each of the square brackets has to be on its own line, otherwise a chess program can not interpret the file. As I'm new to perl, I have to work hard to understand your code, but I will try my best. The line: $line =~ s/ (\d+\. )/\n$1/g; does to my understanding the job of re-inserting the newlines but only with the numbered lines. I have added the line: $line =~ s/ (\[ )/\n$1/g; and I had hoped this will re-install the newlines for the tags, but for no good. If you can post a solution with newlines also for the tags, I would be very thankful to you.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-20 00:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found