Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • Nice trick! It's also possible to count lines from the end, for instance (echo '$-4,$d'; echo wq ) | ed input.txt deletes the last 5 lines. See also the line addressing section of the manual. Commented Mar 10, 2015 at 6:48
  • 2
    However, I think ex (elvis 2.2.0) is better for large files: CPU-bound, few KB of memory used. ed (1.6) run out of memory (over 2 GB memory used) over my 9 GB file. Which explains why you said to use sed or perl for files of unlimited size. ;-) Commented Mar 10, 2015 at 12:20
  • Good point, ex is a more robust line-oriented editor, and it accepts a superset of ed commands (in particular, it understands 10,31d and wq as given above). Commented Oct 10, 2019 at 9:34
  • I was optimistic to see this answer but ran into the same problem as unix.stackexchange.com/questions/66730/… Commented Apr 22, 2021 at 20:37
  • Actually, since my file is only 1.4 G, I then tried your first approach, and that worked. +1. Thanks! Commented Apr 22, 2021 at 20:48