Files

Editing a file and then saving the changes

Here we are making edits to Cargo.toml. We increase the patch version of the crate using inc and then save it back to the file. Use help inc to get more information.

Read the file’s initial contents

  1. > open Cargo.toml | get package.version

Output

0.59.0

Make the edit to the version number and save it.

Note: running this command should work but it will reorder the toml file alphabetically by section.

  1. > open Cargo.toml | upsert package.version { |p| $p | get package.version | inc --patch } | save Cargo.toml

Output none

View the changes we made to the file.

  1. > open Cargo.toml | get package.version

Output

0.59.1


Parsing a file in a non-standard format

Suppose you have a file with the following format.

  1. band:album:year
  2. Fugazi:Steady Diet of Nothing:1991
  3. Fugazi:The Argument:2001
  4. Fugazi:7 Songs:1988
  5. Fugazi:Repeater:1990
  6. Fugazi:In On The Kill Taker:1993

You can parse it into a table.

  1. > open bands.txt | lines | split column ":" Band Album Year | skip 1 | sort-by Year

Output

  1. ━━━┯━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━
  2. # │ Band │ Album │ Year
  3. ───┼────────┼────────────────────────┼──────
  4. 0 Fugazi 7 Songs 1988
  5. 1 Fugazi Repeater 1990
  6. 2 Fugazi Steady Diet of Nothing 1991
  7. 3 Fugazi In On The Kill Taker 1993
  8. 4 Fugazi The Argument 2001
  9. ━━━┷━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━

You can alternatively do this using parse.

  1. open bands.txt | lines | parse "{Band}:{Album}:{Year}" | skip 1 | sort-by Year

Or, you can utilize the headers command to use the first row as a header row like. The only difference would be the headers would match the case of the text file. So, in this case, the headers would be lowercase.

  1. > open bands.txt | lines | split column ":" | headers | sort-by year

Word occurrence count with Ripgrep

Suppose you would like to check the number of lines the string “Value” appears per file in the nushell project, then sort those files by largest line count.

  1. > rg -c Value | lines | split column ":" file line_count | into int line_count | sort-by line_count | reverse

Output

  1. ───┬──────────────────────────────────────┬────────────
  2. # │ file │ line_count
  3. ───┼──────────────────────────────────────┼────────────
  4. 0 crates/nu-source/src/meta.rs 27
  5. 1 crates/nu-protocol/src/value/dict.rs 10
  6. 2 src/commands/config.rs 10
  7. 3 crates/nu_plugin_sys/src/sys.rs 10
  8. 4 src/commands/from_bson.rs 9
  9. 5 src/utils/data_processing.rs 9
  10. 6 src/deserializer.rs 8
  11. 7 src/commands/histogram.rs 7
  12. 8 src/commands/split_column.rs 6
  13. 9 src/data/dict.rs 6
  14. ───┴──────────────────────────────────────┴────────────
  15. ... example output limited due to large output