如何从2个文件中读取随机行并将它们放在一起 [英] how to read a random line from 2 files and put them together

查看:120
本文介绍了如何从2个文件中读取随机行并将它们放在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在bash中尝试了 shuf -n 1文件名,它向我展示了一个随机行,但我希望能够在我自己的网站上使用2个文件和输出结合在一起。



例如我的文件是

 文件1 
one one
five eight
one two three

file 2
canada
usa
netherland

所以输出将是五八美元或one one netherland

有人知道任何可以帮助我开始的代码吗?

解决方案

使用paste,shuf和bash的进程替换:

  paste -d<(shuf -n 1 file1)<(shuf -n 1 file2)

输出(示例):

 
一二三加元


so I tried shuf -n 1 filename in bash and it showed me a random line, but I want to be able to do this on my own website with 2 files and the outputs combined.

for example my files are

file 1
one one
five eight
one two three

file 2 
canada
usa
netherland

so the output would be "five eight usa" or "one one netherland"

Does anyone know any code that can help me start this?

解决方案

With paste, shuf and bash's process substitution:

paste -d " " <(shuf -n 1 file1) <(shuf -n 1 file2)

Output (example):

one two three canada

这篇关于如何从2个文件中读取随机行并将它们放在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆