如何在 BASH 中查找并替换文件每一行的最后一个空格?

Vil*_*age 3 regex bash replace sed

我有一个这样的文件:

Once upon a time there lived a cat.
The cat lived in the forest.
The forest had many trees.
Run Code Online (Sandbox Code Playgroud)

我需要用“@”替换每行的最后一个空格,例如:

Once upon a time there lived a@cat.
The cat lived in the@forest.
The forest had many@trees.
Run Code Online (Sandbox Code Playgroud)

我尝试过sed 's/ .*$/@/g' file.txt,但.*匹配所有内容并删除在那里找到的所有文本。

如何用“@”替换每行最后一个空格?

Alf*_*lfe 5

尝试这个:

sed 's/\(.*\) /\1@/'
Run Code Online (Sandbox Code Playgroud)