Reputation: 357
I'm using a batch file to do some quick Perl search and replace stuff on some files on Windows 7 with Strawberry Perl v5.22.0 installed.
However, I've got one situation I can't figure out how to do.
Sample text (it relates to horse racing and favouritism if you wondering what odd looking data is):
2/2 FIRST PIGEON, A. R. Boyd's, It (D. Mangos) 3 9/9 BROADWAY, M. C. McTigue's, It (S. M. Miller) 4 17/16 CLEM'S PRIZE (WhipsterCrathie), G. D Shand's
Also started: 13/13 Adele Hanover, 11/12 Apollo King, 12/11 Cushan, 4/4 Fortunate, 6/8 Frosty McCoy.
I want to add line breaks before the 'number slash number' on first line BUT want to ignore all the same matches on the second line. So the result would look like:
2/2 FIRST PIGEON, A. R. Boyd's, It (D. Mangos) 3
9/9 BROADWAY, M. C. McTigue's, It (S. M. Miller) 4
17/16 CLEM'S PRIZE (WhipsterCrathie), G. D Shand's
Also started: 13/13 Adele Hanover, 11/12 Apollo King, 12/11 Cushan, 4/4 Fortunate, 6/8 Frosty McCoy.
These sort of patterns happen multiple times in each file.
So, adding in the newline before each number/number is easy enough
perl -i.bak -pe "s#([0-9]+/[0-9]+.*?)#\n\1#g" tmpFile
but this of course applies it to all occurrences.
I've googled for hours and tried all sorts of ways to ignore the matches on the lines beginning with Also started but with no success. I feel this should be easy but can't see how, can someone show me the magic please?
I need a one liner like above to fit in with the batch file please.
Upvotes: 2
Views: 943
Reputation: 126722
I don't understand why so many people try to cram everything into a single regex
Just add a conditional statement modifier, like this. (Note that you should use $1
etc. for the capture groups, not \1
etc.)
perl -i.bak -pe "s#(\d+/\d+)#\n$1#g unless /^Also started/" tmpFile
Upvotes: 4