I have a set of large files of numbers output by some program, that are in a very wide layout -- ie. there are many numbers listed per line separated by spaces -- which is convenient for the program but not for human inspection.
I wanted to 'wrap' the lines at a convenient place. Whilst the numbers are not sequential, they are ordered and so I chose to wrap them such that all the numbers within each 100 range (eg. nnn100 .. nnn199 ) are on a single line.
To clarify, the input (vastly cut down) looks like this:
105 106 107 108 109 110 111 112 113 1115 1116 1117 1118 1119 1120 1121 1122 1123 12345 12346 12347 12348 12349 12350 12351 12353
And I decided to wrap it to look like this:
105 106 107 108 109 110 111 112 113 1115 1116 1117 1118 1119 1120 1121 1122 1123 12345 12346 12347 12348 12349 12350 12351 12353
I though to do this by replacing the space between (n1)\d\d and (n1+1)\d\d with a newline. Hence I tried the following regex:
#! perl -slw use strict; while( <DATA> ) { s[\s(\d+)\d\d\K\s(?=(\d+)\d\d\s)]{ $1 + 1 == $2 ? "\n" : ' ' }ge; print; } __DATA__ 105 106 107 108 109 110 111 112 113 1115 1116 1117 1118 1119 1120 1121 1122 1123 12345 12346 12347 12348 12349 12350 12351 12353
But it does nothing. I've since done the job (with multiple passes due to the fixed-length look-behind limitation) of:
perl -ple"s[(?<=\s(\d{3})\d\d)\s(?=(\d{3})\d\d\s)][$1+1==$2 ? qq[\n] : + ' ' ]ge" in > out
But I still don't understand why the above regex fails?
In reply to Why doesn't this regex work? (Solved!) by BrowserUk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |