Delete every other line in clipboard

Any way to delete every other line of text in clipboard?
Search and replace was able to get me to this point but stuck at removing this other data.

For Example if the string is:
S126SA3
06/12/18 11:22:53 627823 07/12/18 81621
S126SFG
07/16/18 12:22:53 78532645 07/16/18 8153145

End Results: S126SA, S126SFG

Real world results would be at maximum 20 results. Both the text that is wanted to keep and the ones I want removed will be different but still will always be on separate lines.

Thanks!

Use of an Execute JS for Automation action might look something like this:

Every second line of clipboard.kmmacros (19.0 KB)
image

JavaScript for Automation source

(() => {
    'use strict';

    const main = () => {
        ObjC.import('AppKit');

        const xs = lines(strip(clipText()));
        return 1 < xs.length ? (
            xs.reduce((a, x, i) => even(i) ? a : a + x + '\n', '')
        ) : unlines(xs);
    };

    // JXA ------------------------------------------------

    // clipText:: () -> String
    const clipText = () => {
        const v = ObjC.unwrap($.NSPasteboard.generalPasteboard
            .stringForType('public.utf8-plain-text'));
        return Boolean(v) && 0 < v.length ? (
            v
        ) : '';
    };

    // GENERIC FUNCTIONS ----------------------------------
    // https://github.com/RobTrew/prelude-jxa

    // even :: Int -> Bool
    const even = n => 0 === n % 2;

    // lines :: String -> [String]
    const lines = s => s.split(/[\r\n]/);

    // strip :: String -> String
    const strip = s => s.trim();

    // unlines :: [String] -> String
    const unlines = xs => xs.join('\n');

    // MAIN ---
    return main();
})();

Possible that I misread whether you intended the first line of your example to be included or not.

If you add an odd function to the code:

// odd :: Int -> Bool
const odd = n => !even(n);

then you can adjust the main expression from:

xs.reduce((a, x, i) => even(i) ? a : a + x + '\n', '')

to:

xs.reduce((a, x, i) => odd(i) ? a : a + x + '\n', '')

In any case, one or other of these variants should do what you want : -)

Your stated requirement is inconsistent with your "End Results", so I'm not sure exactly what you want. But this simple Macro (no script required) should get you started.


Example Results

image


MACRO:   Get Odd Lines in Text [Example]


#### DOWNLOAD:
<a class="attachment" href="/uploads/default/original/3X/9/b/9bcd357159da9414da3861ac107d49f39eeffe11.kmmacros">Get Odd Lines in Text [Example].kmmacros</a> (7.4 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**

---

### ReleaseNotes

Author.@JMichaelTX

**NOTICE: This macro/script is just an _Example_**

* It is provided only for _educational purposes_, and may not be suitable for any specific purpose.
* It has had very limited testing.
* You need to test further before using in a production environment.
* It does not have extensive error checking/handling.
* It may not be complete.  It is provided as an example to show you one approach to solving a problem.

**REQUIRES:**

1. **KM 8.2+**
2. **macOS 10.11.6 (El Capitan)**
  * KM 8 Requires Yosemite or later, so this macro will probably run on Yosemite, but I make no guarantees.  :wink: 


**MACRO SETUP**

* **Carefully review the Release Notes and the Macro Actions**
  * Make sure you understand what the Macro will do.  
  * You are responsible for running the Macro, not me.  😉
.
* Assign a Trigger to this maro.
* Move this macro to a Macro Group that is only Active when you need this Macro.
* ENABLE this Macro.
.
* **REVIEW/CHANGE THE FOLLOWING MACRO ACTIONS:**
  * ALL Actions that are shown in the magenta color
     * SET Source String

**USE AT YOUR OWN RISK**

* While I have given this limited testing, and to the best of my knowledge it will do no harm, I cannot guarantee it.
* If you have any doubts or questions:
  * **Ask first**
  * Turn on the KM Debugger from the KM Status Menu, and step through the macro, making sure you understand what it is doing with each Action.

---

![image|546x1026](upload://w4iLnVc3ehFR7yblnzJpaCqYyJK.png)

---

### Questions?

Search & Replace Reg Ex

Search: (.*)\R.*\R
Replace: $1, 

You will need to remove the trailing , at the end of the resulting line.

Note: “\R” works only in 10.11+

1 Like