I want to delete 10 random lines from a text file that has 90 lines and then output this to a new file. I've been trying to do this using sed, but I have two problems. I'm using:
sed -i $((1 + RANDOM & 90))d input.txt > output.txt
and then running the command 10 times (I assume there is a better way to do this!)
The first problem I have is that I get the error:
sed: -e expression #1, char 2: invalid usage of line address 0
I assume this has something to do with the fact that it might have already deleted line 1 and it is trying again.
The second problem is that sometimes nothing is written to the output file, even though it worked before using the same command.
Best Answer
You probably wanted to use
RANDOM % 90
rather then&
. That's where the zeroes come from (deleting line 1 is OK, on the next run, the lines will be numbered 1 .. 89).There is a problem, though: The formula could generate the same number several times. To prevent that, use a different approach: shuffle the numbers and pick the first ten:
If you don't like
sed
generating ased
script, you can useprintf
, too: