简体   繁体   中英

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:
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:

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

Is there a way to do this easily in bash? Cheers!

You can do it trivially using the shuf command along with cat , eg

cat toinsert.txt mainfile.txt | shuf

That will combine the lines from toinsert.txt and mainfile.txt in a shuffled order. To write the result back to mainfile.txt , you will need to use an intermediate temporary file, eg

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)

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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