Sed删除“."之后的所有内容.在文件中使用*命令? [英] Sed to remove everything after "." in file using * command?

查看:59
本文介绍了Sed删除“."之后的所有内容.在文件中使用*命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下data.txt:

I have the following 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..

我尝试使用以下sed命令没有成功,这会产生空白文档:

I have tried the using these sed commands without success, which yield a blank document:

sed "s/'.*//" data.txt > cleaned.txt 
sed 's/\.*//' data.txt > cleaned.txt 

推荐答案

要么使用反斜杠对.进行转义以获取文字.,或者使用方括号定义一个角色类别:

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

您尝试了's/\.*//',它是零个或多个文字点",与后跟零个或多个字母的文字点"不同,即's/\..*//'.我还添加了一个 $ 以作好衡量.

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.

这篇关于Sed删除“."之后的所有内容.在文件中使用*命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆