Combining offset lines
-
Hi all – got great advice on my last problem that worked perfectly. On to a new problem!
I have the following text repeating throughout a file.
*AC* *PD* *MD* *HP* *19* *17* *13* *92*
What I am trying to get to is recombining the lines into the correct ‘column’, so that the output end up looking like this:
*AC**19* *PD**17* *MD**13* *HP**92*
The lines to combine together are always 8 lines apart from one another – but I’m not sure how to correctly identify the block and then resort it into the right order. Any guidance from this wise group?
Thanks!
-
I should also mention the text block is scattered throughout other text – but it always starts with AC by itself on a line.
-
This solution makes no assumptions about the contents of each of the 8 4-character fields; they can even be spaces. It does however rigidly require that no trailing whitespace is present on any of the 8 lines.
Fi:
(.{4})\R\R(.{4})\R\R(.{4})\R\R(.{4})\R\R(.{4})\R\R(.{4})\R\R(.{4})\R\R(.{4})\R
Re:\1\5\r\n\2\6\r\n\3\7\r\n\4\8\r\n
Do you know how to run Find & Replace using a regex?
Place the caret at file start, or at least before the first occurrence of these sparse octets you’re combining.
Start with single replaces until you are satisfied it’s doing what you hope, then you can issue a Replace All.
-
@Brennan-OBrien said in Combining offset lines:
but it always starts with AC by itself on a line.
I failed to work that in to my solution, but it may work fine as is.
In any event, the change to the Find expression would be to replace the first parenthesized chunk with
(\*AC\*)
. -
@Neil-Schipper
Thanks, that worked wonders! I had some of the 8th item containing 3 and 4 digits, but changing the last {4} to {5} and {6} respectively solved it. Thank you!! -
@Brennan-OBrien said in Combining offset lines:
changing the last {4} to {5} and {6} respectively solved it
Good to hear. It could also have been dealt with by using
{4,6}
which would handle all 3 cases in one go. (Speaking of which, with this problem fresh in your mind, it might be a good time to peruse the regex documentation just enough to identify the pieces that were used in the solution expressions.)