Nushell has very useful commands to filter lists and tables.
When you have a list with null values, you can use the compact
command to filter out the null values.
With the option --empty
you can also filter out empty items like empty strings, empty lists and empty records.
If you want to filter out rows in a table where a column contains a null value, you can use the compact
command followed by the name of the column.
In the following example you can see how to use the compact
command to filter out null values and empty values from a list:
use std/assert # You can use compact to filter out null items. assert equal ([1 2 null 4 5] | compact ) [1 2 4 5] # With option -e or --empty also empty items like # "" for strings, # [] for lists and # {} for records. assert equal (["hello" "" "world"] | compact --empty) ["hello" "world"] assert equal ([[1 2] [] [3 4]] | compact --empty) [[1 2] [3 4]] assert equal ([{} {name: mrhaki}] | compact -e) [{name: mrhaki}]
The next example shows how to use the compact
command with the name of columns to filter out rows in a table where a column contains a null or empty value:
use std/assert # Create a table with ships and captains. let ships = [ [ship captain]; ["Flying Dutchman" "Davy Jones"] ["Queen Anne's Revenge" "Blackbeard"] ["Black Pearl" null] [null "Norrington"] ["Dauntless" ""] ["" "Soa Feng"] ] # Specify column captain in row to filter out null values. # Rows with null values in other columns are kept. assert equal ($ships | compact captain) [ [ship captain]; ["Flying Dutchman" "Davy Jones"] ["Queen Anne's Revenge" "Blackbeard"] [null "Norrington"] ["Dauntless" ""] ["" "Soa Feng"] ] # Specify column ship to filter out null and empty values. assert equal ($ships | compact --empty ship) [ [ship captain]; ["Flying Dutchman" "Davy Jones"] ["Queen Anne's Revenge" "Blackbeard"] ["Black Pearl" null] ["Dauntless" ""] ] # Multiple colunm names can be used to filter rows with # null or empty column values. assert equal ($ships | compact -e ship captain) [ [ship captain]; ["Flying Dutchman" "Davy Jones"] ["Queen Anne's Revenge" "Blackbeard"] ]
Written with Nushell 0.104.0.