Skip to content

Instantly share code, notes, and snippets.

@kisp
Created February 7, 2024 17:35
Show Gist options
  • Save kisp/1baf20cd6229c646a2fac0942d7bc5ca to your computer and use it in GitHub Desktop.
Save kisp/1baf20cd6229c646a2fac0942d7bc5ca to your computer and use it in GitHub Desktop.
grep for complements

grep for complements

Imagine you have a CLI command cat myfile | ./my-filter, and now you want to see all the lines not selected, essentially negating my-filter. Here is a way to achieve that.

The "grep for complements" command is a useful tool for filtering out lines from a file that have not been selected by a custom filter. This can be achieved using a combination of cat, your custom filter (./my-filter), and grep.

Usage

To use "grep for complements", follow these steps:

  1. Ensure you have a file (myfile) containing the data you want to filter.

  2. Create or implement a custom filter program named my-filter that selects a subset of lines from myfile.

  3. Run the following command in your terminal:

    cat myfile | ./my-filter | grep -F -v -f - myfile

    Replace myfile with the path to your file.

Explanation

  • cat myfile: Reads the contents of the file myfile and outputs them.
  • ./my-filter: Runs your custom filter program, which selects a subset of lines from the input.
  • grep -F -v -f - myfile: grep with the -F option treats patterns as fixed strings, -v selects non-matching lines, and -f - tells grep to use patterns from standard input (piped from the output of ./my-filter). Finally, myfile specifies the file to search for the complement of the selected lines.

This command effectively filters out lines selected by ./my-filter and displays the complement in myfile.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment