diff --git a/crates/nu-cli/tests/commands/sum.rs b/crates/nu-cli/tests/commands/math/sum.rs similarity index 100% rename from crates/nu-cli/tests/commands/sum.rs rename to crates/nu-cli/tests/commands/math/sum.rs diff --git a/crates/nu-cli/tests/commands/mod.rs b/crates/nu-cli/tests/commands/mod.rs index 22febe997..614edd477 100644 --- a/crates/nu-cli/tests/commands/mod.rs +++ b/crates/nu-cli/tests/commands/mod.rs @@ -44,7 +44,6 @@ mod split_by; mod split_column; mod split_row; mod str_; -mod sum; mod touch; mod trim; mod uniq; diff --git a/docs/commands/math.md b/docs/commands/math.md index 0af856efc..fa61dd379 100644 --- a/docs/commands/math.md +++ b/docs/commands/math.md @@ -117,6 +117,13 @@ To get the average of the file sizes in a directory, simply pipe the size column ────────────┴──────── ``` +To get the sum of the characters that make up your present working directory. + +```shell +> pwd | split row / | size | get chars | math sum +50 +``` + ## Errors `math` functions are aggregation functions so empty lists are invalid diff --git a/docs/commands/sum.md b/docs/commands/sum.md deleted file mode 100644 index f36d5b265..000000000 --- a/docs/commands/sum.md +++ /dev/null @@ -1,44 +0,0 @@ -# sum -This command allows you to calculate the sum of values in a column. - -## Examples -To get the sum of the file sizes in a directory, simply pipe the size column from the ls command to the sum command. - -```shell -> ls | get size | math sum -━━━━━━━━━ - value -━━━━━━━━━ - 51.0 MB -━━━━━━━━━ -``` - -To get the sum of the characters that make up your present working directory. -```shell -> pwd | split-row / | size | get chars | math sum -━━━━━━━━━ - -━━━━━━━━━ -21 -━━━━━━━━━ -``` - -Note that sum only works for integer and byte values. If the shell doesn't recognize the values in a column as one of those types, it will return an error. -One way to solve this is to convert each row to an integer when possible and then pipe the result to `sum` - -```shell -> open tests/fixtures/formats/caco3_plastics.csv | get tariff_item | math sum -error: Unrecognized type in stream: Primitive(String("2509000000")) -- shell:1:0 -1 | open tests/fixtures/formats/caco3_plastics.csv | get tariff_item | math sum - | ^^^^ source -``` - -```shell -> open tests/fixtures/formats/caco3_plastics.csv | get tariff_item | str --to-int | math sum -━━━━━━━━━━━━━ - -───────────── - 29154639996 -━━━━━━━━━━━━━ -```