我的 sed RE 有什么问题?找不到模式并替换

fis*_*ree 3 sed regular-expression

我在 Ubuntu 系统上有一个巨大的文本文件,并且有很多行“文档”模式,后跟 25 个随机字符,即

cussion. But we cancelled. That's correct," Fasel said.
The 2021 IIHF Women's World Championships is scheduled for the Russian city of Ufa.
Document TASS0000202asd07eg370012y
Fasel said that the IIHF had cancelled all women's international tournaments this year, including the IIHF Ice Hockey Women's World Championship Division I Group A in Angers, France on April 12-18.
Document TaSS0asfd0200307eg370012y
Nevertheless, the IIHF president pointed out that there was no decision yet about the men's world championships set to open in Switzerland in May.
Document aASS000020200307eg370012y
"We are working normally with the Swiss association and everybody is thinking and hoping that we can organize the world championship in May," Fasel said when asked about new information on that tournament.
Canada reported the first coronavirus case on January 26. Up to now, 54 cases have been confirmed in the country. In late December 2019, a pneumonia outbreak caused by the COVID-19 virus (previously known as 2019-nCoV) was reported in China's city of Wuhan, an economic and industrial megacity with a population of 12 million. The World Health Organization declared the new coronavirus outbreak a public health emergency of international concern, characterizing it as an epidemic with multiple locations. Outside China, the worst affected countries are Iran, Italy and South Korea. Overall, more than 90 other countries, including Russia, have reported confirmed coronavirus cases. WHO says that new coronavirus cases outside China have passed 21,000, and there are over 400 deaths.
Document TASS0fgs20200307eg370012y
Run Code Online (Sandbox Code Playgroud)

我想找到所有匹配的行并用指定的字符串替换该模式,如下所示:

sed -i 's/^Document\s{1}\w{25}\n$/MYLINEBREAK/' textfile.txt
Run Code Online (Sandbox Code Playgroud)

但是,它根本不起作用。

ter*_*don 7

默认情况下,sed使用POSIX基本正则表达式和不理解\s或者\w甚至是{}。它也不知道如何匹配 a\n因为那是该行的结尾。这样做的便携式方法是:

sed 's/^Document [a-zA-Z0-9-]\{25\}$/MYLINEBREAK/' file
Run Code Online (Sandbox Code Playgroud)

几乎与可移植性-E用于启用扩展正则表达式一样:

sed -E 's/^Document\s[a-zA-Z0-9-]{25}$/MYLINEBREAK/' file
Run Code Online (Sandbox Code Playgroud)

其中,至少在 GNU sed(Linux上的那个)上可以让您进一步简化到几乎一开始的样子:

sed -E 's/^Document\s\w{25}$/MYLINEBREAK/' file
Run Code Online (Sandbox Code Playgroud)

请参阅为什么我的正则表达式在 X 中有效但在 Y 中无效?有关不同正则表达式风格的更多详细信息。