Regex 使用SED从文本文件中删除所有单个字母

Regex 使用SED从文本文件中删除所有单个字母,regex,sed,terminal,Regex,Sed,Terminal,我想在Linux中使用SED命令从文本文件中删除所有带有一个字母的单词。例如,我有以下文字: yes said holmes answering the look rather than the words it is so i know all about mccarthy the old man sank his face in his hands god help me he cried but i would not have let the young man come to harm

我想在Linux中使用SED命令从文本文件中删除所有带有一个字母的单词。例如,我有以下文字:

yes said holmes answering the look rather than the words it is
so i know all about mccarthy
the old man sank his face in his hands god help me he cried but
i would not have let the young man come to harm i give you my word
that i would have spoken out if it went against him at the assizes
i am glad to hear you say so said holmes gravely
i would have spoken now had it not been for my dear girl it would
break her heartit will break her heart when she hears that i am
arrested
对于正则表达式,我使用SED命令如下:

sed -E 's/(\s[a-z]\s)/ /g' examplefile > destinationfile

在我运行命令后,结果与之前类似,没有任何变化。我错过了什么?

您的基本问题可能是您的
sed
不理解Perl扩展
\s

另一个问题是,匹配“exabc”中“a”周围的两个空格将“消耗”它们,因此“b”无法匹配。若“c”在行的末尾,它后面就并没有空格,所以也不匹配

如果您的
sed
支持单词边界
\b
,请尝试。它匹配一个空字符串,但仅在其一侧有字母字符的位置匹配

sed 's/\b[a-z]\b//g'
这将留下两个空格,其中删除的单词被两侧的空格包围;有各种各样的方法可以解决这个问题,但从您的需求来看,还不清楚这是否有必要。

使用
\b
代替
\s

\s
匹配空格<代码>\b匹配单词边界。

这可能适合您(GNU-sed):


删除行首、行中或行尾的任何单个字符,并将其替换为前面的空格(或缺少空格)。

好的,您只删除一个字母单词,因此输出将类似,请参阅。无论如何,输出与输入不同。请将该示例输入的所需输出(无说明)添加到问题中(无评论)。
sed -E ':a;s/(^|\s)[a-z](\s|$)/\1/Ig;ta' file