Regex sed-行结束时的删除周期

Regex sed-行结束时的删除周期,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** (GNU sed版本4.2.1) 谢谢这是瞎猜,但我以前在尝试将Windows文件与Linux文件混合使用时

我试图删除文本文件中位于行末尾的句点。有些行末尾有句点,有些行没有:

$cat textfile
sometexthere.123..22.no_period
moretext_with_period.  **<-- remove this period**
no_period_here_either
period.   **<-- remove this period**
(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. 

如果您需要一个
sed
命令来执行此操作,而不需要使用
dos2unix
,这会更改原始文件,您可以执行类似操作(可能需要GNU-sed)

其中,在命令行中键入“
^M
”作为Ctrl+V,后跟Ctrl+M

这将删除“.”,可选地后跟一个回车符,如果CR在原始字符中存在,则替换CR。

sed's/period[.|]*$//g'ts.txt>ts1.txt 输入文件: 这里有一些。123..22。没有句号 带句点的更多文本。
这里也没有句号 句号

输出文件: 这里有一些。123..22。没有_ moretext_与_ 这里也没有句号

sed -r 's/\.$//'

这也适用于删除最后一个句点。

看到
$cat-vet textfile
的输出可能会有所启发。为什么要使用
g
修饰符?这似乎很有效。dos2unix似乎对该文件做了一些“操作”,因此命令sed's/\.$//g'现在可以工作了。。。你到底做了什么?我的文本文件是一个很大的csv文件中的一列,以防人们怀疑。Linux文件的每一行末尾都是
\n
,而Windows文件的每一行末尾都是
\r\n
。您的正则表达式不匹配,因为句点不直接位于
\n
\r
的旁边)。我在此解决方案中添加了一个示例。一旦它被审查,你应该能够看到它。如果您注意到这些异常,检查的一个好方法是执行
cat-v文件名
。这将显示所有非打印字符,以便它们可见。@Jaypal:
cat-如果您有一个支持它的
cat
版本,则文件名更好。除了以可打印格式显示不可打印字符外,它还用
^I
替换制表符,并在每行末尾添加
$
cat-vet filename
,其中
-A
选项不可用:我相信你有一个多余的'\'sed-r's/\.$/'为我工作。
sed -E 's/\.(^M?)/\1/' testfile
sed -r 's/\.$//'