regex - sed - 删除行尾的句点

标签 regex sed

我正在尝试删除文本文件中行尾的句点。有些行的末尾有句点,有些则没有:

$cat textfile
sometexthere.123..22.no_period
moretext_with_period.  **<-- remove this period**
no_period_here_either
period.   **<-- remove this period**

我试过这个,但它似乎不起作用:
sed 's/\.$//g' textfile > textfile2

(GNU sed 版本 4.2.1)

谢谢

最佳答案

这是黑暗中的一个镜头,但我之前在尝试将 Windows 文件与 Linux 文件混合时遇到了这个问题。 Windows 添加了一个额外的 \r在每个换行符上(除了标准 \n )您是否尝试过使用 dos2unix?

[user@localhost ~]$ cat testfile
abc
def.
[user@localhost ~]$ sed 's/\.$//g' testfile
abc
def.
[user@localhost ~]$ dos2unix testfile
dos2unix: converting file testfile to UNIX format ...
[user@localhost ~]$ sed 's/\.$//g' testfile
abc
def
[user@localhost ~]$ 

例如这个 -
[user@localhost ~]$ cat temp.txt 
this is a text created on windows
I will send this to unix
and do cat command.

[user@localhost ~]$ cat -v temp.txt 
this is a text created on windows^M
I will send this to unix^M
and do cat command. 

关于regex - sed - 删除行尾的句点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8317447/

相关文章:

bash - 在 sed 命令中使用变量的值

bash - 列出文本文件中所有不同字符的脚本

javascript - 调整正则表达式以删除尾随小数点+零(如果没有分数)

正则表达式匹配多个点

正则表达式提取查询字符串

regex - 在两种情况下 SED 在 MacOS 上失败 : invalid repetition count(s) and bad flag in substitute command

regex - 从文件中删除包含非英语 (Ascii) 字符的行

c++ - std::regex 的编译时构建

C# - 在另一个字符串中查找一组字符串中的一个的最快方法

json - 使用 sed 提取 json 值