Sample Header Ad - 728x90

Unix & Linux Stack Exchange

Q&A for users of Linux, FreeBSD and other Unix-like operating systems

Latest Questions

1 votes
2 answers
132 views
Line editor ed saves some filenames in single quotes followed by one space
I'm following *The Unix Programming Environment* exercises and have encountered some weird issues. While going through the `ed` exercise, when you just start `ed`, type in the exercise, then do w poem It saves in the filesystem as `'poem '` I tested another file, start `ed`, type in some words, w ra...
I'm following *The Unix Programming Environment* exercises and have encountered some weird issues. While going through the ed exercise, when you just start ed, type in the exercise, then do w poem It saves in the filesystem as 'poem ' I tested another file, start ed, type in some words, w raistlin and it saves as 'raistlin ' with the same result (filename in single quotes followed by one space), though I tested on a third, and if I start off with the file name it saves as a normal file. Has anyone else seen these results? I know it's a little esoteric, I think I'm probably 1 of 7 people on the whole earth using ed this weekend. I'm using Manjaro Arch.
Mark Scheck (280 rep)
Nov 28, 2020, 08:06 AM • Last activity: Jul 29, 2025, 08:56 AM
2 votes
3 answers
165 views
Is it possible to filter a set of lines through an external command in ed?
In `ed`, one can retrieve the output of a command into the current buffer by using `r !COMMAND`. One can also write a set of lines to the input of a command by using `1,3w !COMMAND`. However, I cannot determine how to do both simultaneously. r 1,3w !sort 1,3w !sort: No such file or directory **Is it...
In ed, one can retrieve the output of a command into the current buffer by using r !COMMAND. One can also write a set of lines to the input of a command by using 1,3w !COMMAND. However, I cannot determine how to do both simultaneously. r 1,3w !sort 1,3w !sort: No such file or directory **Is it possible to do this in ed?**
merlin2011 (4139 rep)
May 18, 2018, 05:47 PM • Last activity: Jul 23, 2025, 07:44 AM
5 votes
3 answers
126 views
With ed, how do you fold and unfold lines?
Given the following file: ``` foo bar baz this and that ``` then I can easily "unfold" those lines with global substitution with ed(1): ``` g/ /s//\ /g ``` which gives: ``` foo bar baz this and that ``` But what's a smooth way "fold lines"? I can't use the `\n` or `\ ` pattern in a `g/re/s` idiom. C...
Given the following file:
foo bar baz
this and that
then I can easily "unfold" those lines with global substitution with ed(1):
g/ /s//\
/g
which gives:
foo
bar
baz
this
and
that
But what's a smooth way "fold lines"? I can't use the \n or \ pattern in a g/re/s idiom. Currently I'm using the following approach: 1. Insert spaces. 2. Join.
1
+,++s/^/ /
--,.jp
++,+++s/^/ /
--,.jp
,p
to get back:
foo bar baz
this and that
but it's pretty clunky and I wonder if there's a better way!
mbigras (3472 rep)
Feb 20, 2025, 08:48 PM • Last activity: May 25, 2025, 09:59 PM
2 votes
2 answers
1048 views
Running an ed script from within ed
I have created a script file called "cleanup" which contains a series of regular expressions that clean up an ed file(s) of blank spaces, trailing white-space, blank lines etc. I run it as follows: ed [name-of-file] < [name-of-script] I sometimes want to run the script on the file I am currently edi...
I have created a script file called "cleanup" which contains a series of regular expressions that clean up an ed file(s) of blank spaces, trailing white-space, blank lines etc. I run it as follows: ed [name-of-file] < [name-of-script] I sometimes want to run the script on the file I am currently editing from within ed. I am unsure of the syntax I would need to do that. Here is an example script: g/^ */s/// # Remove blank spaces at the beginning of a line g/ *$/s/// # Remove trailing whitespace at end of line g/ */s// /g # Remove additional spaces between words g/^$/d # delete blank lines g/\(‘‘\|’’\)/s//"/g # Remove curly braces g/\(“\|”\)/s//"/g g/\(‘\|’\)/s//'/g # idem ,p Q
edman (588 rep)
Jul 14, 2021, 02:56 PM • Last activity: May 5, 2025, 10:50 PM
1 votes
1 answers
79 views
Why can't I run an interactive command in awk in a pipeline?
Consider the following commands: ``` $ awk 'BEGIN { system("ed") }' q $ echo hello | awk 'BEGIN { system("ed") }' ? $ ``` In the first case, I was able run ed and then stay in the editor and manually quit with the q command; but in the second, ed automatically exited. Why doesn't the ed interactive...
Consider the following commands:
$ awk 'BEGIN { system("ed") }'
q
$ echo hello | awk 'BEGIN { system("ed") }'
?
$
In the first case, I was able run ed and then stay in the editor and manually quit with the q command; but in the second, ed automatically exited. Why doesn't the ed interactive session work in the second command? Is there any way to run the AWK system function to call an interactive command while in a pipeline?
mbigras (3472 rep)
Mar 20, 2025, 01:04 AM • Last activity: Mar 20, 2025, 01:34 AM
1 votes
1 answers
54 views
How do you put fzf CTRL-R into rlwrap ed?
I sourced the /usr/share/doc/fzf/examples/key-bindings.bash file so fzf works in Bash; but how do you get that to work with the `rlwrap ed` approach?
I sourced the /usr/share/doc/fzf/examples/key-bindings.bash file so fzf works in Bash; but how do you get that to work with the rlwrap ed approach?
mbigras (3472 rep)
Mar 17, 2025, 07:08 AM • Last activity: Mar 17, 2025, 09:39 AM
1 votes
1 answers
39 views
With ed, reach to the next occurrence
Consider a file with an AsciiDoc [ordered list][1] like this: ``` . one + ---- echo hello # arbitrary lines... echo world ---- . two . three ``` How do you move the code block to some arbitrary place in the list (like two)? In words, I want to "reach from + to the next quad dash". I played with the...
Consider a file with an AsciiDoc ordered list like this:
. one
+
----
echo hello
# arbitrary lines...
echo world
----
. two
. three
How do you move the code block to some arbitrary place in the list (like two)? In words, I want to "reach from + to the next quad dash". I played with the problem for a bit and can solve in two steps: 1. Get to the start of the code block of the one item: /one/;/----/. 1. Attach the code block to another list item—that is, in ed, move the range from previous line (the +) to the next ---- to the target (two list item in this case): -,/----/m/two/. Which yields:
. one
. two
+
----
echo hello
# arbitrary lines...
echo world
----
. three
But I wonder if there's a nicer way!
mbigras (3472 rep)
Mar 2, 2025, 07:07 AM • Last activity: Mar 15, 2025, 08:13 AM
1 votes
1 answers
58 views
How to use tabs with `rlwrap ed`?
On my macOS and Linux systems, I run ed with `alias ed='rlwrap ed'`. On macOS with rlwrap 0.46.1 (2022) I can freely create files with tabs like: ``` $ ed a foo bar ``` but on my Linux systems with rlwrap 0.43 (2016) I can't use tabs unless I escape like this: ``` $ ed a foo bar ``` I know I should...
On my macOS and Linux systems, I run ed with alias ed='rlwrap ed'. On macOS with rlwrap 0.46.1 (2022) I can freely create files with tabs like:
$ ed
a
foobar
but on my Linux systems with rlwrap 0.43 (2016) I can't use tabs unless I escape like this:
$ ed
a
foobar
I know I should get around to upgrading my Linux servers; but rlwrap is currently at the latest according to apt. Is there some rlwrap option that will give me back my tabs? I tried searching through the rlwrap manpage for "tab"; but didn't find anything.
mbigras (3472 rep)
Feb 27, 2025, 07:00 AM • Last activity: Feb 27, 2025, 09:01 PM
4 votes
2 answers
1726 views
sed regexp address range minus (or plus) number of lines
I have multiple ICS files that contain something like: ``` ... PRIORITY:4 DESCRIPTION:this was a triumph COMPLETED:20180101T160000 ... ``` I am in interested in addressing the `DESCRIPTION` key. The key `DESCRIPTION` can occur anywhere in the ICS file. It is _not_ always followed by the key `COMPLET...
I have multiple ICS files that contain something like:
...
PRIORITY:4
DESCRIPTION:this
  was
  a
  triumph
COMPLETED:20180101T160000
...
I am in interested in addressing the DESCRIPTION key. The key DESCRIPTION can occur anywhere in the ICS file. It is _not_ always followed by the key COMPLETED. Using ed, I would do it like this:
/DESCRIPTION/;/^[^ ]/-1p
which results in:
DESCRIPTION:this
  was
  a
  triumph
However, sed does not seem to have that capability:
sed -n '/DESCRIPTION/,/^[^ ]/-1p' filename
results in > sed: -e expression #1, char 22: unknown command: `-' Is there a way to do what I want using sed? ## Solving this particular problem This problem can be solved by:
sed -n '/DESCRIPTION:/,/^\S/ { /\(DESCRIPTION\|^\s\)/p }' example.ics
But this feels very unwieldy and verbose. awk also does not seem to support /begpat/,/endpat/-1, so you would end up with something like:
BEGIN { endpat = "^(DESCRIPTION|\\s)" }
/^DESCRIPTION/, $0 !~ endpat {
    if ($0 ~ endpat) {print}
}
Stefan van den Akker (352 rep)
Sep 17, 2019, 12:21 PM • Last activity: Feb 23, 2025, 08:13 AM
7 votes
2 answers
1926 views
Why does Debian not include POSIX-specified commands like bc and ed by default?
[POSIX.1-2001](https://pubs.opengroup.org/onlinepubs/009695399/) [Utilities](https://pubs.opengroup.org/onlinepubs/009695399/idx/utilities.html) and [POSIX.1-2008](https://pubs.opengroup.org/onlinepubs/9699919799/) [Utilities](https://pubs.opengroup.org/onlinepubs/9699919799/idx/utilities.html) both...
[POSIX.1-2001](https://pubs.opengroup.org/onlinepubs/009695399/) [Utilities](https://pubs.opengroup.org/onlinepubs/009695399/idx/utilities.html) and [POSIX.1-2008](https://pubs.opengroup.org/onlinepubs/9699919799/) [Utilities](https://pubs.opengroup.org/onlinepubs/9699919799/idx/utilities.html) both list the commands bc and ed to be part of POSIX. Yet, in a brand new Debian installation (version 10 for example), these commands are missing by default:
$ bc
bash: bc: command not found
$ ed
bash: ed: command not found
Why does Debian not include these commands by default? Of course, I can install them with and I did that.
apt-get install bc ed
The bc binary is only 87K in size. The entire package including the man page and documentation is only 209K in size. Similarly the ed binary is only 55K in size. The entire package is only 93K in size. What good reason is there not to include these tiny packages even though they are specified in POSIX?
Lone Learner (170 rep)
Sep 12, 2020, 07:00 AM • Last activity: Feb 23, 2025, 08:08 AM
1 votes
2 answers
87 views
Can the ed editor be configured to automatically create backup files?
Is there a way to have `ed` create backups of the file currently loaded into the buffer. On occasion I have inadvertently overwritten my file and found it's not recoverable. I.e. making a backup file with a tilde appended to the original file name (e.g., `myfile.txt~`), which would contain the previ...
Is there a way to have ed create backups of the file currently loaded into the buffer. On occasion I have inadvertently overwritten my file and found it's not recoverable. I.e. making a backup file with a tilde appended to the original file name (e.g., myfile.txt~), which would contain the previous version of the file.
edman (588 rep)
Jul 18, 2024, 05:55 AM • Last activity: Feb 8, 2025, 09:23 AM
2 votes
2 answers
692 views
Removing more than two consecutive newlines with ed
Input file: 1 line1\ 2 line2\ 3 line3\ 4 \ 5 line4\ 6 \ 7 \ 8 line5\ Desired output: 1 line1 2 line2 3 line3 4 5 line4 6 7 line5 Is it possible with POSIX `ed`? Removing the trailing backslashes is easy, but how can I also handle consecutive blackslashes as expected output?
Input file: 1 line1\ 2 line2\ 3 line3\ 4 \ 5 line4\ 6 \ 7 \ 8 line5\ Desired output: 1 line1 2 line2 3 line3 4 5 line4 6 7 line5 Is it possible with POSIX ed? Removing the trailing backslashes is easy, but how can I also handle consecutive blackslashes as expected output?
user28512 (31 rep)
Dec 9, 2012, 05:05 PM • Last activity: Dec 16, 2024, 08:56 AM
1 votes
1 answers
244 views
A use case for the 'r' suffix to the substitution command in GNU Ed?
I am running GNU Ed on Fedora Linux. The [GNU Ed manual][1] cryptically refers to an `'r'` suffix for the `s` command, to be distinguished from the `'r'` command. All it says about it is the following: >The suffix `'r'` causes the `re` of the last search to be used instead of the re of the last subs...
I am running GNU Ed on Fedora Linux. The GNU Ed manual cryptically refers to an 'r' suffix for the s command, to be distinguished from the 'r' command. All it says about it is the following: >The suffix 'r' causes the re of the last search to be used instead of the re of the last substitution (if the search happened after the substitution). I cannot see its usefulness especially if one uses Ed with rlwrap, with which one can easily modify the last substitution. Can someone provide a use case for 'r' as a suffix?
edman (588 rep)
Jul 26, 2024, 05:40 AM • Last activity: Jul 26, 2024, 10:49 AM
3 votes
1 answers
157 views
Showing whitespace in Ed
In Ed I can test for whitespace with this regex: `g/ *$/p`. I don't suppose there is a way of showing whitespace, perhaps by passing the contents of the buffer to another shell command?
In Ed I can test for whitespace with this regex: g/ *$/p. I don't suppose there is a way of showing whitespace, perhaps by passing the contents of the buffer to another shell command?
edman (588 rep)
Jul 25, 2024, 06:16 AM • Last activity: Jul 25, 2024, 09:30 AM
0 votes
2 answers
201 views
ed(1) adds ^M to every line of my file
I am working on automating a WordPress install. While editing wp-config.php file, I need to replace 8 lines containing `put your unique phrase here` with Salt generated by WordPress.org servers. So, I used the top answer from [this tread][1] which tells me to run this script: ```bash #!/bin/sh SALT=...
I am working on automating a WordPress install. While editing wp-config.php file, I need to replace 8 lines containing put your unique phrase here with Salt generated by WordPress.org servers. So, I used the top answer from this tread which tells me to run this script:
#!/bin/sh

SALT=$(curl -L https://api.wordpress.org/secret-key/1.1/salt/) 
STRING='put your unique phrase here'
printf '%s\n' "g/$STRING/d" a "$SALT" . w | ed -s wp-config.php
This works just fine, as it replaces the required lines with proper salt. However, it also appends a ^M at the end of every string in wp-config.php, except the newly added ones. Now it looks like this:
*/^M
^M
define('AUTH_KEY',         '|%)Y1>vzXGRbE?`FFZX3Mq|ur?tN/,R&%@)juSc?f@bNPYm~P=aF&Fl?!HGF4V}a');
define('NONCE_SALT',       '6;KHO6=[Ta-h7.2B@3SadaIinz/6!O=GQWE(@r(<3L+X,/:[du%Q');
/**
**/^M
^M
/**^M
 * WordPress Database Table prefix.^M
I don't know much about ed(1), so any help will be much appreciated.
user377738
Oct 17, 2019, 02:40 PM • Last activity: Jul 19, 2024, 08:37 PM
2 votes
1 answers
328 views
Deleting specific line numbers in ed in a single command
In `ed` one can delete a line number by typing its address followed by the `delete` command, as in `1d`. To delete a range of lines, one uses the comma, as in `1,4d`. One can also delete lines with specific content as in `g/Ted/d`. Is there a way to specify the deletion of non-sequential lines *in a...
In ed one can delete a line number by typing its address followed by the delete command, as in 1d. To delete a range of lines, one uses the comma, as in 1,4d. One can also delete lines with specific content as in g/Ted/d. Is there a way to specify the deletion of non-sequential lines *in a single command*? I.e. let's say I wanted to delete lines 1 and 3 and 8 at one time without typing: 1d 3d 8d
edman (588 rep)
Jun 24, 2022, 05:08 PM • Last activity: May 3, 2024, 02:56 PM
1 votes
2 answers
241 views
How to modify this `printf` code to accept input in first to last order, or flip output?
Code below can, for example ... [anony@mous-pc ~]$ (printf 'g?%s?m0\n' 008 006 004 002 ; printf 'wq\n') | ed -s file.txt ... take all of numbers within ... [anony@mous-pc ~]$ cat file.txt 005 003 110 069 002 008 004 245 009 007 006 ... and move desired numbers (or other strings) to top, **BUT in rev...
Code below can, for example ... [anony@mous-pc ~]$ (printf 'g?%s?m0\n' 008 006 004 002 ; printf 'wq\n') | ed -s file.txt ... take all of numbers within ... [anony@mous-pc ~]$ cat file.txt 005 003 110 069 002 008 004 245 009 007 006 ... and move desired numbers (or other strings) to top, **BUT in reverse order** of executed code ... [anony@mous-pc ~]$ cat file.txt 002 004 006 008 005 003 110 069 245 009 007 ... just so long as you give the order in reverse, the order will be first to last (opposite order), or ... others may call it top to bottom formatting ... so one more time, to achieve first to last order I had to execute this way ... [anony@mous-pc ~]$ (printf 'g?%s?m0\n' 008 006 004 002 ; printf 'wq\n') | ed -s file.txt > BUT, how can one input their numbers (or strings) in the order they > intended for being first to last and still achieve first to last > order? > > If not possible to change inputted results, would sed be able to > change output results and flip or reverse them before saving > file.txt?
Anonymous (533 rep)
May 21, 2019, 03:22 AM • Last activity: Apr 22, 2024, 09:41 AM
0 votes
3 answers
98 views
Edit inside an HTML tag with ed(1)
Consider my humble _`hello.html`_ file, edited with mighty ed: ``` $ ed hello.html 28 ,p Hello world! ``` What's your general approach to edit inside that *title* HTML tag (bonus if you can edit inside any HTML tag)? I tried a regular expression that matches inside the tag: ``` s/>.*/>My new title/p...
Consider my humble _hello.html_ file, edited with mighty ed:
$ ed hello.html 
28
,p
Hello world!
What's your general approach to edit inside that *title* HTML tag (bonus if you can edit inside any HTML tag)? I tried a regular expression that matches inside the tag:
s/>.*/>My new title/p
My new title
u
.
Hello world!
But, sadly, you can see that I chopped my tag (and it would be way too much work to type out that _``_ bit every time!). For further education, I browsed through Software Tools in Pascal page to 174—see https://archive.org/details/softwaretoolsinp00kern/page/174/mode/1up?view=theater page—and discovered the _&_ special character that helpfully reaches the _middle_ of the sentence:
s/world/& again/p
Hello world again!
But, that's not quite right, since I want to substitute the middle, not just reach the middle.
mbigras (3472 rep)
Feb 6, 2024, 06:22 AM • Last activity: Feb 6, 2024, 10:49 AM
6 votes
1 answers
332 views
Why can't I print this regex range with ed(1)?
I'm totally mystified why I can't print this range with ed *when there are two ranges*—see *file2.tf* file—; but I can print when there is only *one range*—see *file1.tf* file—and I can print with the gsed (GNU sed on macOS) command; but I can't print with ed. Please consider my shell session and cl...
I'm totally mystified why I can't print this range with ed *when there are two ranges*—see *file2.tf* file—; but I can print when there is only *one range*—see *file1.tf* file—and I can print with the gsed (GNU sed on macOS) command; but I can't print with ed. Please consider my shell session and clarify my misconceptions:
$ ed -s file1.tf <<<',n'
1	# some comment
2	module "hello_world" {
3	  source = "./mydir"
4	}
5	# another comment
$ ed -s file1.tf <<<'/module.*world/,/}/p'
module "hello_world" {
  source = "./mydir"
}
$ ed -s file2.tf <<<',n'
1	# some comment
2	module "hello_world" {
3	  source = "./mydir"
4	}
5	# another comment
6	# some comment
7	module "hello_again" {
8	  source = "./anotherdir"
9	}
10	# another comment
$ ed -s file2.tf <<<'/module.*again/,/}/p'
?
$ gsed -n '/module.*again/,/}/p' file2.tf
module "hello_again" {
  source = "./anotherdir"
}
Update: The reverse direction works; but I'm not sure why:
$ ed -s file2.tf <<<'?module.*again?,?}?p'
module "hello_again" {
  source = "./anotherdir"
}
Update 2: The ?? approach doesn't actually work as intended (if there are *three sections* like in *file3.tf* file example) and see answer for an explanation.
$ ed -s file3.tf <<<,n
1	# some comment
2	module "hello_world" {
3	  source = "./mydir"
4	}
5	# another comment
6	# some comment
7	module "hello_again" {
8	  source = "./anotherdir"
9	}
10	# another comment
11	# some comment
12	module "hello_yet_again" {
13	  source = "./yetanotherdir"
14	}
15	# another comment
$ ed -s file3.tf <<<'?module.*hello_again?,?}?p'
module "hello_again" {
  source = "./anotherdir"
}
# another comment
# some comment
module "hello_yet_again" {
  source = "./yetanotherdir"
}
mbigras (3472 rep)
Jan 5, 2024, 05:54 AM • Last activity: Jan 5, 2024, 07:23 PM
29 votes
4 answers
23625 views
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 cryptic error message is more useful here tha...
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 cryptic error message is more useful here than just exiting. This behavior leads many new users into the following sort of interaction: > $ ed > hello > ? > help > ? > exit > ? > quit > ? > ^C > ? > ^C > ? > ? > ? > ^D > $ su > # rm -f /bin/ed Such a tragic waste—easily avoidable if ed simply agreed to be interrupted. Another stubborn program exhibiting similar behavior is less which also doesn't appear to have much reason to ignore C-c. Why don't these programs just take a hint?
lily (413 rep)
Aug 17, 2014, 12:25 AM • Last activity: Nov 29, 2023, 12:19 PM
Showing page 1 of 20 total questions