简体   繁体   English

用 sed 替换最后一次出现的空格

[英]Replace last occurrence of space with sed

I need to replace the last occurrence of space in an input file, using sed .我需要使用sed替换输入文件中最后一次出现的空格。

What I came up with is我想出的是

sed "s/([ ])[0-9]*$/,/g"

However, it does not seem to want to remember the space which it's supposed to replace.但是,它似乎不想记住它应该替换的空间。 Running the command without round brackets works fine (for what it's supposed to do - replace the space and the chain of numbers).运行没有圆括号的命令可以正常工作(对于它应该做的事情 - 替换空格数字链)。 When I add the brackets, it does nothing.当我添加括号时,它什么也不做。

Yes, I am aware of this solution, however when trying to pass \\1 to sed , it screams that "\\1 not defined in the RE".是的,我知道这个解决方案,但是当试图将\\1传递给sed ,它会尖叫“\\1 未在 RE 中定义”。

Anyone care to help?有人愿意帮忙吗? It seems to be a simple issue, I'd be glad to know the solution.这似乎是一个简单的问题,我很高兴知道解决方案。

这似乎“第一次”起作用(是的)......

$ sed -e 's/ \([^ ][^ ]*\)$/,\1/' /etc/hosts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM