【发布时间】:2020-11-27 18:23:01
【问题描述】:
您好,我正在编写一个简单的 BASH 来删除段落中由单个空格分隔的任何单词的连续重复出现,并将输出重定向到标准输出,这是我得到了多远。
文件1
**double double toil and trouble
fire burn and cauldron bubble bubble
tomorrow and tomorrow and tomorrow
creeps in this this petty pace from day toto day**
echo `<file1` | sed -e 's/\b\([a-z ]\+\)\1/\1/g' | cat > file2
这是在一行中输出,如下所示。
double toil and trouble fire burn and cauldron bubble tomorrow and tomorrow creeps in this petty pace from day to day
并且遗漏了一些东西,因为它没有正确删除事件。
【问题讨论】:
-
只要使用
sed 's/\b\([a-z ]\+\)\1/\1/g' file > file2 -
问题是明天的第 3 行被删除,这是不正确的,因为在 'tomorrow' 之后有 'and' & 第 4 行 'toto' 被删除,它没有被空格分隔,所以不应该被认为是连续的重复。
-
应用标签前请阅读标签说明。其中一半放错地方了!