简体   繁体   English

如何将文件的行随机插入另一个文本文件?

[英]How do I insert lines of a file randomly into another text file?

I'm looking for a way to insert all lines from one file to another file randomly.我正在寻找一种将所有行从一个文件随机插入另一个文件的方法。 Elaborating, lets say I have 2 files:详细说明,假设我有 2 个文件:
toinsert.txt

insert1
insert2
insert3

and
mainfile.txt

line1
line2
line3
line4
line5
...

The resultant file should look like the following where lines from toinsert.txt and mainfile.txt are randomly mixed up:结果文件应如下所示,其中toinsert.txtmainfile.txt中的行随机混合:

line1
insert1
line2
line3
insert2
...
insert3
...

Is there a way to do this easily in bash?有没有办法在 bash 中轻松做到这一点? Cheers!干杯!

You can do it trivially using the shuf command along with cat , eg您可以使用shuf命令和cat轻松完成此操作,例如

cat toinsert.txt mainfile.txt | shuf

That will combine the lines from toinsert.txt and mainfile.txt in a shuffled order.这会将toinsert.txtmainfile.txt中的行以打乱的顺序组合起来。 To write the result back to mainfile.txt , you will need to use an intermediate temporary file, eg要将结果写回mainfile.txt ,您需要使用中间临时文件,例如

cat toinsert.txt mainfile.txt | shuf > tmp; mov -f tmp mainfile.txt

(of course make sure you don't already have a tmp file or it will be overwritten) (当然要确保你还没有tmp文件,否则它会被覆盖)

Let me know if you have further questions.如果您还有其他问题,请告诉我。

You are basically generating 3 random numbers with no repeats in the range of number of lines of the other file.您基本上是在生成 3 个随机数,在另一个文件的行数范围内没有重复。

Like:喜欢:

mainfilecnt=$(wc -l <mainfile.txt)
toinsert=$(wc -l <toinsert.txt)
# copy input to output
cp mainfile.txt output.txt
# get as many random numbers as lines to insert in the range of lines
shuf -i 1-"$mainfilecnt" -n "$toinsertcnt" |
sort |
# join numbers with lines
paste - toinsert.txt |
# Reverse to insert from the last line
tac |
# we have number of line to insert to and a line.
# So insert it at that line number.
while IFS=$'\t' read -r num line; do
     sed -i -e "${num}i"<(printf "%s\n" "$line") output.txt
done

or like:或喜欢:

# get as many random numbers as lines to insert in the range of lines
shuf -i 1-"$mainfilecnt" -n "$toinsertcnt" |
# sort reverse for inserting
sort -r |
# generate GNU sed script to insert numbers
sed 's/.*/&R'toinsert.txt'/' |
# Use xargs to pass generated sed script back to sed
xargs -0 -I{} sed {} mainfile.txt

tested on repl 在 repl 上测试

The scripts above have a bug/feature that the line will not be inserted as the first line, only as second.上面的脚本有一个错误/功能,该行不会作为第一行插入,仅作为第二行插入。 These are just very short scripts I have written to show the method - you should refine them for what you actually need.这些只是我为展示该方法而编写的非常简短的脚本——您应该根据您的实际需要对其进行改进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM