sed delete but save deleted output into other file

Hi guys, I am currently using this to save first 50 lines into top50.txt and delete them from list.txt ... it's 2 commands:

head -n 50 list.txt > top50.txt && sed -i "1,50 d" list.txt

I want to change that so it's 1 command - whereby sed removes the first 50 lines as above but that which is removed gets saved to top50.txt - so no need for the head command as I am doing....

Can that be done? Thanks in advance!

I'm not at all sure it can be done with sed. awk, yes. May I ask why? You already have one line that works, presumably.

awk 'NR<51 {print $0>"file1";next } NR>50 {print $0>"file2"} ' inputfile

For sed, try:

sed -n '51,${p;d;};wtop50.txt' list.txt > rest.txt

Hi, thanks for those solutions. I will try them out.

The purpose is to optimize the method I am doing. I think it's not as efficient as it could be.