Bash。如何将文本文件中的随机行分配给变量?
Bash. How to assign random line from text file to a variable?
我有一个包含很多行的文本文件。我需要做的:
- 舒夫txt.txt
- 从 shuf 输出读取第一行到变量 $line
如何在 bash 脚本中用一行表示?
现在是这样的:
shuf txt.txt -o aaa.txt
n=$(head -n 1 aaa.txt)
rm -rf aaa.txt
你可能会注意到,它不是很好
听起来像是家庭作业。这里有一些提示。
将变量分配给命令的结果:
x=`command`
管道赋值:
x=`command1 | command2`
将 command1 输出的第一行赋给一个变量:
x=`command1 | head -n1`
您可以使用 shuf
轻松做到这一点:
n=$(shuf -n1 file)
↳https://www.gnu.org/software/coreutils/manual/html_node/shuf-invocation.html
堆栈上的相关问题overflow.com:What's an easy way to read random line from a file in Unix command line?
我有一个包含很多行的文本文件。我需要做的:
- 舒夫txt.txt
- 从 shuf 输出读取第一行到变量 $line
如何在 bash 脚本中用一行表示?
现在是这样的:
shuf txt.txt -o aaa.txt
n=$(head -n 1 aaa.txt)
rm -rf aaa.txt
你可能会注意到,它不是很好
听起来像是家庭作业。这里有一些提示。
将变量分配给命令的结果:
x=`command`
管道赋值:
x=`command1 | command2`
将 command1 输出的第一行赋给一个变量:
x=`command1 | head -n1`
您可以使用 shuf
轻松做到这一点:
n=$(shuf -n1 file)
↳https://www.gnu.org/software/coreutils/manual/html_node/shuf-invocation.html
堆栈上的相关问题overflow.com:What's an easy way to read random line from a file in Unix command line?