What I want to do is print a random line from text file A into text file B WITHOUT it choosing the same line twice. So if text file B has a line with the number 25 in it, it will not choose that line from text file A
I have figured out how to print a random line from text file A to text file B, however, I am not sure how to make sure it does not choose the same line twice.
echo "$(printf $(cat A.txt | shuf -n 1))" > /home/B.txt
2
Answers
That’s not really "random", then. Never mind.
Please try the following
awk
solution – I think it does what you’re trying to achieve.First part (
grep
) prints difference ofA
andB
to stdout, i.e. lines present inA
but absent inB
:-F
— Interpret PATTERNS as fixed strings, not regular expressions.-x
— Select only those matches that exactly match the whole line.-v
— Invert the sense of matching.-f FILE
— Obtain patterns from FILE.Second part (
shuf -n 1
) prints random line from stdin. Output is appended toB
.