Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Given a file of humongous size, I can edit with ed, then issue the following command.


Which will print out all lines containing 'real'.

What I want to do is print the first 20.

I know sed, grep, head, etc. But I want to improve my ed skills.

share|improve this question
I don't know ed, but | head -n20? – Rob Feb 5 '13 at 18:35
Hi, trying to find solution only using ed. – Bryan Hunt Feb 5 '13 at 18:49

You can specify ranges before your commands in the form from,to.


would print lines 1-4.

share|improve this answer

Here's one way:

ed -s infile <<IN
v/PATTERN/d               # delete all lines NOT matching PATTERN
,w !head -n20             # pass the addressed lines as input to shell command
u                         # undo deletion: not needed if you don't save changes
q                         # quit editor
share|improve this answer

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.