首页 > 解决方案 > 删除空行后从文本文件中选择随机行

问题描述

标签: bashshell

解决方案


Where does the output of your first sed command go? Based on what you have, it goes to stdout. It is definitely going to stdout within the $(...) sub-process. This means they are written straight out to the outer sub-process as command line arguments.

The shuf command is trying to get something from stdin, not arguments from the command line.

What might work (I have not tried this), is to pipe. Try this

sed '/^\s*$/d' input.txt | shuf -n 1 >output.txt

Hope this helps


推荐阅读