英文:
how to redirect stdout to a file in NuShell?
问题
在NuShell中,我们可以如下方式将某些命令的输出重定向到文件:
ls > fileList.txt
请注意,NuShell使用与zsh/bash类似的 > 符号来执行重定向操作。
英文:
how to redirect some command output to a file in NuShell?
In zsh/bash we can redirect output of ls command to a file as below
ls > fileList.txt
how can we do the same in NuShell?
答案1
得分: 1
要将内容重定向/保存到文件中,可以使用 save 命令(参见手册)。
但是,save 命令接受字符串作为输入,而 ls 命令提供的是记录。您可以通过定义方式将这些记录转换为字符串。
一种方式是,如果您主要关心外观,可以显式将表格呈现为在您的 shell 中显示的方式,使用 table 命令(参见手册,其中甚至将 ls | table 列为示例之一):
ls | table | save fileList.csv
但是,如果您更关心传达的数据,可以使用 to 将其重新格式化为您选择的数据类型(参见格式列表)。例如,要将其重新格式化为 CSV,请使用 to csv,要使用 HTML,请使用 to html,要使用 JSON,请使用 to json 等等。还有一个 to text,它只是将所有记录逐行列出,带有它们的键名前缀。其手册页面 甚至将 ls | to text 列为示例之一。
ls | to csv | save fileList.csv
# 或者
ls | to html | save fileList.html
# 或者
ls | to json | save fileList.json
# 或者
ls | to text | save fileList.txt
# 等等。
英文:
To redirect/save into a file, use the save command (see the manual).
However, save takes strings as input, while ls provides records. You can convert those records into strings by defining how.
One way, if your primary interest lies in saving the looks, is to explicitly render the table as shown in your shell using the table command (see the manual which even lists ls | table as one of the examples):
ls | table | save fileList.csv
But if you are more interested in the data conveyed, re-format it into a datatype of your choice using to (see a list of formats). For example, to re-format it as CSV use to csv, for HTML use to html, for JSON use to json etc. There is also to text which simply lists all records line by line with their key names prepended. Its manual page even lists ls | to text as one of the examples.
ls | to csv | save fileList.csv
# or
ls | to html | save fileList.html
# or
ls | to json | save fileList.json
# or
ls | to text | save fileList.txt
# etc.
答案2
得分: 0
以下命令将ls输出保存为json格式,并以精确格式检索。
ls | 转换为json | 保存 -f fileList.json
打开 fileList.json
使用--force选项来覆盖文件。
命令help save是终端上的好朋友:
> 帮助 保存 06/06/2023 10:50:48 PM
保存文件。
搜索词: 写入, 写入文件, 追加, 重定向, 文件, io, >, >>
用法:
> 保存 {标志} <文件名>
...更多信息在此处...
...更多信息在此处...
示例:
将字符串保存到当前目录中的foo.txt
> '保存我' | 保存 foo.txt
将字符串追加到foo.txt的末尾
> '追加我' | 保存 --追加 foo.txt
将记录保存到当前目录中的foo.json
> { a: 1, b: 2 } | 保存 foo.json
将运行中程序的stderr保存到foo.txt
> 执行 -i {} | 保存 foo.txt --stderr foo.txt
将运行中程序的stderr保存到单独的文件
> 执行 -i {} | 保存 foo.txt --stderr bar.txt
英文:
Below command saved the ls output in json format and retrieved in exact format.
ls | to json | save -f fileList.json
open fileList.json
use --force option to overwrite file.
command help save is your friend on terminal:
> help save 06/06/2023 10:50:48 PM
Save a file.
Search terms: write, write_file, append, redirection, file, io, >, >>
Usage:
> save {flags} <filename>
...more info here...
...more info here...
Examples:
Save a string to foo.txt in the current directory
> 'save me' | save foo.txt
Append a string to the end of foo.txt
> 'append me' | save --append foo.txt
Save a record to foo.json in the current directory
> { a: 1, b: 2 } | save foo.json
Save a running program's stderr to foo.txt
> do -i {} | save foo.txt --stderr foo.txt
Save a running program's stderr to separate file
> do -i {} | save foo.txt --stderr bar.txt
答案3
得分: 0
根据最新的 Nu(我写这篇回答时版本为 0.84)更新这个答案:
Nushell 现在具有重定向操作符:out> 和 out+err>,类似于 sh-shell 的 1> 和 2>&1 等。可以按如下方式使用:
> 10 + 20 >out t.t
> cat t.t
30
重定向操作符与 save 相比有以下区别:
- 重定向
out+err>(缩写为o+e>)可以将两个流捕获到同一个文件中。save f.f --stderr f.f拒绝将两个流保存到同一个文件中。 - 重定向操作符不需要前置的
| save。它们是表达式中的后缀操作符。 - 重定向操作符始终覆盖文件。它们没有追加选项。
- 与
save一样,重定向操作符的输入必须是字符串或通过to json转换为字符串,如上所建议。
英文:
To update this answer based on more recent Nu (v 0.84 as I write this):
Nushell now has redirection operators: out> out+err>, similar to sh-shells 1> 2>&1 etc. Can be used as follows:
> 10 + 20 >out t.t
> cat t.t
30
Redirection operators compared to save.
- redirection
out+err>(abbreviatedo+e>) can capture both streams to a single file.save f.f --stderr f.frefuses to save both streams to same file. - redirection operators do not require the preceeding
| save. They are, er, postfix operators in an expression. - redirection operators always overwrite the file. They do not have an append option.
- like
save, the input for redirection operators must be string or converted to string viato jsonas suggested above.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论