1

So I have a list of indexes in a file called indexes.txt. I want to read every index in that file and delete the corresponding line from another file called input.txt. So far I have this implementation that works:

while read -r index
do
    sed -i "${index}d" $input.txt
done < "indexes.txt"

However, this implementation does not scale well at all. My index.txt file has 12500 lines and input.txt has about 300,000. I think deleting each line and saving the input.txt each time is what takes so much time (a couple seconds for each index). Is there a way to do this more efficiently?

Edit: Sorry, forgot to mention that the indexes in indexes.txt are unique, strictly decreasing integers.

Jack
  • 21
  • 2
  • Welcome to SO, please do mention sample of Input and sample of output in your post with code tags. – RavinderSingh13 Apr 10 '18 at 17:54
  • Use `awk`. Create an array whose keys are the contents of the index file. Then for each line in `input.txt`, check if the line number is in the array. – Barmar Apr 10 '18 at 17:56
  • Your code also doesn't even work, because after each iteration the line numbers in the file change. – Barmar Apr 10 '18 at 17:57
  • It does work if the numbers are listed strictly monotonically. But `"$input.txt"` should be properly quoted, and you should only run `sed` once. See also my "antipattern" answer in the duplicate. – tripleee Apr 10 '18 at 18:00
  • "sed 's%$%d%' indexes.txt | sed -i -f - input.txt" worked perfectly for me. Thanks! – Jack Apr 10 '18 at 18:22

0 Answers0