gpt4 book ai didi

linux - 多串命令输出只保存一串

转载 作者:太空狗 更新时间:2023-10-29 11:49:26 24 4
gpt4 key购买 nike

我有一个 bash 命令来查找子目录中的最大文件。我想将输出保存到一个变量中,以便与其他命令一起使用。

bigile=$(find/path/to/directory -type f -exec du -Sh {} + | sort -rh | head -n 1)

不幸的是,这会同时保存文件大小和文件路径。

echo $file
216K /path/to/directory/bigfile

如果我将变量传递给将文件作为输入的命令,我会看到一个错误:

wc -lm $file
wc: cannot access '216K': No such file or directory
6333 217649 /home/path/to/directory/bigfile

显然,我得到了我需要的输出,但我不想要错误消息。如何只保存 find 命令输出的第一个字符串?

谢谢!!!

最佳答案

能否请您尝试关注并告诉我这是否对您有帮助。

bigile=$(find /path/to/directory -type f -exec du -Sh {} + | sort -rh | awk 'FNR==1{print $2}')

说明: 只需在 sort 命令后添加 awk 并有 FNR==1 条件即可确保仅第一行被拾取,然后也打印该行的第二个字段。

关于linux - 多串命令输出只保存一串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46391522/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com