Questions tagged [ed]

For questions about the editor ed, a scriptable line-oriented text editor, originally written by Ken Thompson for the Multics and Unix operating systems. Use this tag for questions about ed itself or questions about issues arising from using the ed editor.

The ed utility is a line-oriented text editor that uses separate command and input modes. In command mode, the input characters are interpreted as commands, and in input mode, they are interpreted as text.

External reference

ed specification (The Open Group Base Specifications Issue 7, 2018 edition)

79 questions
29
votes
10 answers

How to remove multiple newlines at EOF?

I have files that end in one or more newlines and should end in only one newline. How can I do that with Bash/Unix/GNU tools? Example bad file: 1\n \n 2\n \n \n 3\n \n \n \n Example corrected file: 1\n \n 2\n \n \n 3\n In other words: There should…
Bengt
  • 741
  • 1
  • 8
  • 15
26
votes
4 answers

Why can't ed be exited with C-c?

The program ed, a minimal text editor, cannot be exited by sending it an interrupt through using Ctrl-C, instead printing the error message "?" to the console. Why doesn't ed just exit when it receives the interrupt? Surely there's no reason why a…
Lily Chung
  • 383
  • 1
  • 3
  • 8
12
votes
1 answer

What text editor was used by Thompson and Ritchie in the writing of Unix?

I think that it's probably qed, but I can't find any sources to confirm or deny this. Also, ed has existed for the entire duration of Unix, and sam was written in the early 1980s. Is it known what most people used at Bell Labs from the Unix era to…
occamsrazor
  • 153
  • 6
12
votes
8 answers

sed in-place line deletion on full filesystem?

Due to an application bug as yet undiagnosed, I have several hundred servers with a full disk. There is one file that has been filled up with duplicate lines—not a log file, but a user environment file with variable definitions (so I can't just…
Wildcard
  • 35,316
  • 26
  • 130
  • 258
11
votes
5 answers

Is the `ed` editor useful for any purpose besides emergencies and batch editing?

I'm wondering if the ed editor is useful any longer for interactive editing. I know that the diff command can produce patches as ed scripts and that ed is a last resort when the baud rate is super slow. But are there any other reasons to learn and…
dan
  • 4,007
  • 5
  • 26
  • 34
10
votes
9 answers

sed : have a range finishing with the last occurrence of a pattern (greedy range)

Take the following file : $ cat f1 stu vwx yza uvw xyz abc abc def ghi def ghi jkl ghi jkl mno jkl mno pqr mno pqr stu pqr stu vwx stu vwx yza To print all lines from the first one containing abc to the first one containing mno with GNU sed : $ sed…
ChennyStar
  • 1,319
  • 9
  • 22
10
votes
2 answers

How to write a literal dot in ed?

What would be the right way to write a line containing only . to a file in ed? The only way I came up with is something like this: a . s/^$/./
Staven
  • 223
  • 1
  • 6
9
votes
2 answers

How would I replace two or more blank lines with a single blank line in Ed?

In Ed I can do a search to replace all blank lines as follows: g/^$/d This deletes all blank lines. But what if I wish to delete two or more blank lines and keep 1? For example: Line 1 Line 2 Line 3 Becomes: Line 1 Line 2 Line 3
Edman
  • 472
  • 2
  • 9
9
votes
1 answer

How can I insert a lone dot on a line using ed's "insert" command?

How may I insert a lone dot on a line with the ed editor? Normally, a dot on a line by itself ends the insertion; can it somehow be escaped?
Brennan Vincent
  • 229
  • 1
  • 4
8
votes
8 answers

How to cut a file starting from the line in which a certain pattern occurs?

I have a gazillion files which need to be reduced in size. I found that most (not all) files have an end section which can be cut without losing information: Data 1 Data 2 something_unimportant_here END DATA Rubbish 1 Rubbish 2 How can I edit a…
Ned64
  • 8,486
  • 9
  • 48
  • 86
8
votes
2 answers

Regex `/pattern/g` and ed `:g/pattern/`: which came first, and why `g`?

The g option (e.g. s/pattern/replacement/g) for many tools that use regex-style pattern matching and the :g command in ed, ex, vi, and vim have pretty similar usage and meaning: match the given regex "globally", i.e., don't stop after the first…
Kyle Strand
  • 719
  • 7
  • 15
7
votes
7 answers

Insert text N lines before the last line

I want to insert a new line in two lines before the last line. So if my original file is: 1 2 3 4 5 The result should be 1 2 3 New line 4 5
Gautam
  • 173
  • 1
  • 4
7
votes
5 answers

Display only the penultimate (second last) row of a text

I have a poem with an unknown number of rows and I want to display only the penultimate one. What command should I use?
Elena
  • 71
  • 1
  • 2
6
votes
1 answer

How do I move the last 3 lines of a pipeline to the top of the output with ed?

I have a small AWK script that summarizes the output of another program, and I am trying to print that summary before the command itself, but I cannot figure out a good way to do it. My naive first solution was running the command twice, once to get…
Brent
  • 63
  • 5
6
votes
2 answers

ed command: Delete from line 1 until the first blank line

I came across this code from a textbook; the book only says that 1,/^[ ]*$/d means “Delete from line 1 until the first blank line.” What does each character specifically mean? ed $1 << EOF 1,/^[ ]*$/d w q EOF
Jack Chen
  • 159
  • 1
  • 1
  • 6
1
2 3 4 5 6