Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re^2: Substitution don't work with a special inputfile

by OldChamp (Acolyte)
on Aug 24, 2015 at 16:00 UTC ( #1139703=note: print w/replies, xml ) Need Help??

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

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.

Replies are listed 'Best First'.
Re^3: Substitution don't work with a special inputfile
by poj (Abbot) on Aug 24, 2015 at 16:28 UTC

    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 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;

      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.

        $line =~ s/\n/ /g;

        I don't understand this statement in poj's code: why are all newlines being changed to spaces?

        $line =~ s/ (\d+\. )/\n$1/g;

        This statement seems to speak to a requirement to add newlines before every  1.  23.  345. pattern, but I can't see where this has ever been discussed before. Please give more info on this.

        It would help if you could post a short example of an input file and a corresponding file representing the exact output you want from it. By short, I mean that most of the data in the input file you have already posted seems redundant; please try to keep example data short and sweet.

        And once again, the statement that the result of a piece of code is "no good" is not helpful; in what way is it no good? You should say "With this code and this input data, I want to get 'XXX' and I am getting 'YYY'."

        Give a man a fish:  <%-{-{-{-<

        Your addition should have worked but anyway try this

        #!perl # Aufruf: perl 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;

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2022-09-29 03:59 GMT
Find Nodes?
    Voting Booth?
    I prefer my indexes to start at:

    Results (125 votes). Check out past polls.