List or drop matching lines

Can be done with grep in the shell, of course, but I do it quite a lot, and find it a little quicker to experiment with the help of the simple options of a Keyboard Maestro action. ( Saves me checking man grep for simple things at the command line, and lets me keep to the familiar Javascript dialect of Regular Expressions :- )

List or drop matching lines.zip (10.8 KB)

Custom Keyboard Maestro Plug-in

NAME

  • List or drop matching lines

VERSION

  • 0.1

SYNOPSIS

  • Returns a subset of the input lines

REQUIREMENTS

  • Yosemite
    • The core script listOrDropMatchingLines.sh is mainly written in Javascript for Applications

OPTIONS

  • Source clipboard or variable:
  • Include:
    • ONLY lines matching pattern
    • All EXCEPT matching lines
    • ALL lines
  • Matched line
    • contains
    • is
    • is not
    • begins with
    • ends with
    • matches Regex
  • Pattern
    • ( the literal text or regex pattern to be matched )
  • Ignore case ?
  • Remove blank lines from output ?
    • Can be used in combination with the Include: ALL lines option to simply strip out blank lines from a text
    • Also allows blank lines to pass through regardless of matching

INSTALLATION

  • Drag the .zip file onto the Keyboard Maestro icon in the OS X toolbar.
  • (if updating a previous version of the action, first manually remove the previous copy from the custom actions folder)
    • ~/Library/Application Support/Keyboard Maestro/Keyboard Maestro Actions

CONTACT

6 Likes

Sorry post in an old thread, but would it be possible to have the PATTERN be dynamic? it is currently hardcoded. Ideally I'd like to be the result of a Prompt Action

Haven't looked at this for a while – have you tried placing a %Variable%name% token in the pattern field ?

Hi. Find this could be very useful.

Is it possible just to output TRUE or FALSE instead of the matches?

I'll try to describe my problem:

I just need to know if a particular set of words is present in a line (or sentence)...

Can you help me accomplish that?

I think you'd have to show, as usual, a couple of samples of:

  • typical inputs
  • expected outputs

and explain the broader context of what you are trying to do, and where this fits into it.

Hello, thank you for your reply. It does not work with the %Variable%pattern%, but neither does it work with regular text. Given the macro is 7 years old it might not be compatible with the latest KM

aaaa
bbbb
cccc
aaa
aaaaaa
11111

when typing aaa, the expected output would be just the lines containing aaa, but nothing happens

I probably won't be able to look into that this week, but in the meanwhile, perhaps a partitioning macro would do what you need ?

e.g.
Partitioned lines.kmmacros (5.5 KB)

1 Like

FWIW, inspired by Rob's solution, a Haskell example:

Partitioned lines.kmmacros (3.3 KB)

Haskell Code:

import Data.Bifunctor
import Data.List

main :: IO ()
main =
  interact $
    unlines
      . uncurry (<>)
      . bimap ("Hits:" :) ("Misses:" :)
      . partition (isInfixOf "aaa")
      . lines
1 Like

Thank you so much for these macros guys! Thanks for taking the time to write and reply to this post :heart:

1 Like

Thank you Rob!

I used this technique to "remove" non-matching lines from the clipboard:

EDIT: I haven't added anything new here, but I thought I should post here anyway to help other users who are looking for a macro that removes (keeps/maintains) lines that meet certain conditions. Kind of SEO :).

1 Like