answer to the new version of the question:
we mix file lines and we save in the file перемешанные :
$ shuf файл > перемешанные
take the specified число lines from the file перемешанные (from the beginning of the file) and save the вырезанные files to the file:
$ head -n число перемешанные > вырезанные
we take перемешанные lines from the file, starting with число+1 and save to the original файл :
$ tail -n +$((число+1)) перемешанные > файл
Delete the file that is no longer needed:
$ rm перемешанные
if you want to keep the same order of lines as in the source file, then you need to add numbering ( nl ), sorting ( sort ) and deleting numbers ( cut ). here without comments, only commands (see details below, in response to the previous version of the question):
$ nl файл | shuf > перемешанные $ head -n число перемешанные | sort -n | cut -f 2- > вырезанные $ tail -n +$((число+1)) перемешанные | sort -n | cut -f 2- > файл $ rm перемешанные
The given sequences of commands can be written in one line if you use the pee program from the moreutils package.
option if the line order is not important:
$ shuf файл | pee "head -n число > вырезанные" "tail -n +$((число+1)) > файл"
option if the row order is important:
$ nl файл | shuf | pee "head -n число | sort -n | cut -f 2- > вырезанные" "tail -n +$((число+1)) | sort -n | cut -f 2- > файл"
answer to the previous version of the question:
team
$ shuf файл
will generate stdout strings from файл a in random order. and the team
$ shuf -n число файл
will limit this output to the specified число m lines.
if you want to get the number of lines that makes up the difference between the number of lines in the file and the specified число m, then in the previous command, instead of the число you must substitute the structure that calculates this difference - $(($(cat файл | wc -l) - число)) :
$ shuf -n $(($(cat файл | wc -l) - число)) файл
All received can be saved to a new file (it’s alas, it’s not possible), by adding a redirection - > новый.файл :
$ shuf -n $(($(cat файл | wc -l) - число)) файл > новый.файл
in the received new file the lines will follow in random order. if you want to keep the same line order as in the source file, you can number the lines beforehand (for example, using the nl program) and then, at the end, sort ( sort ) and delete these numbers (for example, using the cut program) :
$ nl файл | shuf -n $(($(cat файл | wc -l) - число)) | sort -n | cut -f 2- > новый.файл