1

I want Nu to find some files for me, filter them and pass them to an external program. For example: "Revert all files whose name contains 'GENERATED' with Git." I tried several variants, but none of them work:

# Doesn't pass the files:
ls --all --full-paths **/* | find GENERATED | git checkout --

# Returns an error: "Cannot convert record<name: string, type: string, size: filesize, modified: date> to a string"
ls --all --full-paths **/* | find GENERATED | each { |it| git checkout -- $it }

# Returns an error for every file: "error: Path specification »?[37m/home/xxx/com.?[0m?[41;37mgwt?[0m?[37mplugins.?[0m?[41;37mgwt?[0m?[37m.eclipse.core.prefs?[0m« does not match any file known to git" [Manually translated into English]
ls --all --full-paths **/* | find GENERATED | each { |it| git checkout -- $it.name }

What is the right way to do that in Nu?

I know how to do that with Bash. This question is just about Nu. :-)

1 Answer 1

1

The most concise and precise way to do it would be this:

ls -af **/* | where name =~ 'GENERATED' | each { ^git checkout -- $in.name }

This uses the where command to filter the name column (which in this case contains the full path and filename because of the -af flags) based on a regex – in your case 'GENERATED'.

The caret in front of the git command is there to ensure you call the external git command. It's good practice to do this if you definitely want to call an external. If you leave it off, there's a chance it'll call an internal version of the command. E.g. ls calls Nushell's internal ls command while ^ls will call the external ls command. Docs: https://www.nushell.sh/book/escaping.html

It also uses the $in value, which contains the results of the pipeline. You don't need to pass it into the each closure as a parameter – it's automatically available. Docs: https://www.nushell.sh/book/pipelines.html

You can use find instead, but it's less precise because it searches across all of the columns in a table of results instead of targeting a specific column. You also need to be careful to strip ansi codes, so you're probably better off sticking to where.

ls -af **/* | find 'GENERATED' | get name | ansi strip | each { ^git checkout -- $in }

or

ls -af **/* | find 'GENERATED' | update name { $in | ansi strip } | each { ^git checkout -- $in.name }

Note, I've not tested this with git checkout, but I did try some other git commands and all the above worked.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .