Python sed 删除“.”之后的所有内容。在文件中使用 * 命令?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18602234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sed to remove everything after "." in file using * command?
提问by Novice
I have the following data.txt:
我有以下 data.txt:
95 flour.
47 water.s
etc..
I need to remove everything after the period . in the file to yield something like this:
我需要在期间后删除所有内容。在文件中产生这样的东西:
95 flour
47 water
etc..
I have tried the using these sed command without success, which yield a blank document:
我已经尝试使用这些 sed 命令但没有成功,这会产生一个空白文档:
sed "s/'.*//" data.txt > cleaned.txt
sed 's/\.*//' data.txt > cleaned.txt
采纳答案by Adam Rosenfield
Either escape the .
with a backslash to get a literal .
, or use brackets to define a character class:
要么.
使用反斜杠转义 以获得文字.
,要么使用方括号定义字符类:
sed 's/\..*$//' data.txt > cleaned.txt
sed 's/[.].*$//' data.txt > cleaned.txt
You tried 's/\.*//'
, which is "zero or more literal dots", which is different from "literal dot followed by zero or more of anything", i.e. 's/\..*//'
. I also added a $
for good measure.
您尝试过's/\.*//'
,这是“零个或多个文字点”,这与“文字点后跟零个或多个任何内容”不同,即's/\..*//'
. 我还添加了一个$
很好的衡量标准。
回答by Novice
This seemed to work:
这似乎有效:
sed "s/[.].*//" data.txt > cleaned.txt
I would be interested to know how this can be done alternatively in bash and python, if anyone wouldn't mind sharing?
我很想知道如何在 bash 和 python 中交替完成,如果有人不介意分享的话?
Thanks!
谢谢!
回答by MK.
This is the simplest:
这是最简单的:
sed "s/\..*//"
And this is, I think, one of the best ways of doing it (better than pure bash or Python).
我认为这是最好的方法之一(比纯 bash 或 Python 更好)。