如何使用 sed 从字符串中删除小数?
我有以下字符串作为示例: 前任。 “Abandoned 16 1.10 2.62 3.50”
我想将此结果通过管道传输到 sed 并删除所有十进制数字,留下以下内容: 前任。 “Abandoned 16”
我使用以下命令:sed 's/.//g' 这显然不起作用。
有人可以让我知道如何在 sed 中使用通配符来删除任何匹配“.”的内容吗?
谢谢
I have the following string as an example:
ex. "Abandoned 16 1.10 2.62 3.50"
I would like to pipe this result to sed and remove all decimal numbers to leave me with the following:
ex. "Abandoned 16"
I was using the following command: sed 's/.//g'
which apparently doesn't work.
Can someone let me know how to use the wildcard character with sed to remove anything matching ".".
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你还没有说你想用空白做什么,但是怎么样
You haven't said what you want to do with the whitespace, but how about
使用 awk 会更容易,至少对我
来说 echo "Abandoned 16 1.10 2.62 3.50" | awk '{print $1FS$2}'
但之后的数字列表是随机的吗?
如果是这样,这也有效
echo "Abandoned 16 1.10 2.62 3.50" | sed -r 's/\s([0-9]+)\.([0-9]+)//g'
请注意
\s
捕获空格,并且小数点之前和之后的数字被保存,所以如果你想保留它们并用它们做一些事情,你可以分别使用\1
和\2
访问它们为什么抓住白色空间?想象一下,如果在您的示例中 16 出现在 3.50 之后,您将返回
Abandoned [5spaces*] 16
*我只能在此
this would be easier with
awk
, at least for meecho "Abandoned 16 1.10 2.62 3.50" | awk '{print $1FS$2}'
but is the list of numbers random afterwards?
if so, this works too
echo "Abandoned 16 1.10 2.62 3.50" | sed -r 's/\s([0-9]+)\.([0-9]+)//g'
note that
\s
catches the white space, and that the numbers before and after the decimal are saved, so if you want to retain them and do something with them you can access them with\1
and\2
respectfullyWhy catch the white sapce? well imagine if 16 came after 3.50 in your example you would then return
Abandoned [5spaces*] 16
*I can only insert one space in this
<textarea>
使用 awk 解决方案循环覆盖输入并跳过其中包含句点的条目
Trowing in a awk solution that loops ovewr the input and skips entries with a period in them