Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I am trying to use ed to edit the first line of a large file, but I have to wait for ed to read all lines. Is there any way I could stop ed from reading the whole file and start editing immediately, with the current line being the first line?

share|improve this question
ed or ex can't do this. Unix utils expect to write to a file, not go in and fiddle with bytes of the file, or the file allocation table definitions. sed '1d' file > newFile will be about as fast as you can get without writing your own C program. Also, ed and ex (at least the ones I have worked with) are limited to the size of the file they can work on, often times by space available in /tmp (or /var/tmp) or in ex for the internal setting of directory. (You know ex is the steam version of vi|vim? Do :set all to find the value for directory). Good luck. – shellter Jul 12 '12 at 3:10
Thank you for your explanation. – abc Jul 13 '12 at 11:06

1 Answer 1

As noted by shellter ed cannot do this. Most editing operations require that the file is rewritten, only replacements can be done in-place, in that case you could use hexedit(1).

The least memory hungry way is to use sed operations, e.g. to replace August with June on the first line:

sed '1s/August/June/' FILE > NEWFILE
share|improve this answer
Thanks. I am aware of the solution using sed. I particularly find the -i flag useful in order to avoid creating temp files. I was looking for an ed solution, but I will give hexedit a try. Thank you. – abc Jul 13 '12 at 11:12
@sina, -i does create a temporary file, but sed handles it transparently in the background. See info sed and search for -i. – Thor Jul 13 '12 at 11:38
This is bad news. But it does make sense considering shelter's comment and your explanations. Thanks for pointing it out. – abc Jul 14 '12 at 9:41

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.