nushell

In nushell how can I combine where clauses


I have this nushell query:

open ./s3_actions.csv | where actions !~ 'Acl' | where actions !~ 'Cors' | where actions !~ 'Job' | where actions !~ 'Retention' | where actions !~ 'Legal' | where actions !~ 'Payment' | where actions !~ 'Policy' | where actions !~ 'Configuration' | where actions !~ 'Replicate' | where actions !~ 'AccessPoint'

Is there a way I could combine all the where actions !~ ... clauses into one big list ?

The closest I got was here but I don't know how to do nested loops, both would have the same $it variable.


Solution

  • You didn't provide any sample data to validate against, but I assumed something like:

    actions,element,node,something
    Acl Forward Bar Baz,52206,19075,71281
    Legal Stack,31210,21366,52576
    Configuration Help Doc,13684,65142,78826
    Help Doc,48488,44794,93282
    Baz Bar Foo,13140,60057,73197
    Foo Baz Bar,62892,63836,126728
    ,64022,54909,118930
    Abc,50240,16109,66348
    Def Bar Foo,18680,17847,36526
    Acl Bar Vegas,55553,22953,78506
    Taco Hamburger Sushi,30311,42345,72656
    

    It's possible there's another way for your particular data source, but the following line (split to multiple lines for improved readability) returns the same results as the multiple where clauses in your question:

    open ./s3_actions.csv | where {|it| 
        (["Acl", "Cors", "Job", "Retention", "Legal", "Payment", "Policy", "Configuration", "Replicate", "AccessPoint"] | 
        reduce -f true { |action,acc|
            ($it.actions !~ $action) && $acc
        })
    }
    

    Explanation:

    Ultimately, what you are asking for is a way to "reduce" a "list of where actions" down to a single value, true or false. Look to the reduce command for that.

    In this particular case:

    And yes, because there are two blocks here (for where and reduce), you'll need to use a different name for the arguments for each.