Concatenate/Merge Every Other Line with a Delimiter String

Hi, I have the following input:

Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
…

I want to transform this to:

Line 1: Line 2
Line 3: Line 4
Line 5: Line 6
Line 7: Line 8
…

How can I archive this? Thanks!

2 Likes

How about this? Note - make sure you copy your input text into the clipboard before running the macro.

Download Macro(s): Test Combine Lines.kmmacros (6.5 KB)

Macro-Image

Keyboard Maestro Export

Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System Information
  • macOS 10.14.6
  • Keyboard Maestro v10.2
2 Likes

As an alternative to @tiffle's method, you can also do it directly with a Regular Expression "Search and Replace". So to transform text on the clipboard:

image

Search: (.+)\R(.+\R?)
Replace: \1%Space%\2

3 Likes

:+1:

I have the following input ... I want to transform this to ... how can I achieve this ?

I think this wins a prize for the best-asked question I have seen for a long time ...

( and quickly reaped a good harvest of responses ...)

4 Likes

Doesn't that just double each line, : separating the items?

Thanks ! Reading too fast.

1 Like

This problem is very straightforward, and there are all sorts of methods of getting it done.

I'd normally do this sort of job with RegEx but thought I'd stretch my legs a little and get some practice in with other tools.

(And – @Nige_S beat me to the RegEx anyway :sunglasses:).


Sed Version


Download: Concatenate Every Other Line of Data String (Sed) v1.00.kmmacros (6.8 KB)

Macro-Image

Keyboard Maestro Export

Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.

System Information
  • macOS 10.14.6
  • Keyboard Maestro v10.2

Awk Version


Download: Concatenate Every Other Line of Data String (Awk) v1.00.kmmacros (6.8 KB)

Macro-Image

Keyboard Maestro Export

Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.

System Information
  • macOS 10.14.6
  • Keyboard Maestro v10.2

Perl Version


Download: Concatenate Every Other Line of Data String (Perl) v1.00.kmmacros (6.8 KB)

Macro-Image

Keyboard Maestro Export

Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.

System Information
  • macOS 10.14.6
  • Keyboard Maestro v10.2

JavaScript Version


Download: Concatenate Every Other Line of Data String (JavaScript) v1.00.kmmacros (7.6 KB)

Macro-Image

Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.

System Information
  • macOS 10.14.6
  • Keyboard Maestro v10.2

Keyboard Maestro Native Version (Array)

** I used a different method than @tiffle's.


Download: Concatenate Every Other Line of Data String (KM Native Array) v1.01.kmmacros (9.2 KB)

Macro-Image

Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.

System Information
  • macOS 10.14.6
  • Keyboard Maestro v10.2

6 Likes

Thank you for the multitude of solutions! (I‘ll use the regex, but it was helpful to see other possible solutions.)

2 Likes

If, incidentally, what was essentially needed was not so much the next line as the integer successor, e.g.

Line 1: Line 2
Line 202: Line 203
Line 7: Line 8
Line 4: Line 5
Line 501: Line 502
Line 6: Line 7
Line 788: Line 789
Line 8: Line 9

Then, of course, you could turn to a KM CALCULATE field or token, or a script:

Integer successor of each line number ?.kmmacros (3.3 KB)

2 Likes

I went with the RegEx because it handles a final half-line:

Line 1: Line 2
Line 3: Line 4
Line 5: Line 6
Line 7: Line 8
Line 9

...which may be out of scope, but you never know...

Turns out it wasn't that difficult to do the same with KM arrays:

Concat Test.kmmacros (6.1 KB)

Image

I'm still dithering as to whether it's good practice to return similar transforms with a trailing newline when there wasn't one in the original string. Thoughts, anyone?

...is using the wrong increments. I think you need to create a Collection of "every odd integer between 1 and local_DataStr[0]" and work from that.

1 Like

Hi, @qrt. Here's one more using a native bash script:

Download: Concatenate Every Other Line of Data String (bash).kmmacros (6.0 KB)

Macro-Image


Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.

System Information
  • macOS 13.4 (22F66)
  • Keyboard Maestro v10.2

3 Likes

Hey Nige,

Thanks for catching that!

I must have been seeing what I expected to see and not reality... My excuse is that it was late, and I was tired... :sunglasses:

I used a single conditional to fix the problem and have replaced the original KM-Array version with v1.01

-Chris

Was thinking about it yesterday, and a fix that remains true to the spirit of your method would be to:

foreach from local_LineNum from 1 to (local_DataStr[0] / 2)
   append local_newDataStr with local_DataStr[local_LineNum * 2 -1]:local_DataStr[local_LineNum * 2]\n
end
1 Like