【发布时间】:2021-02-28 18:37:55
【问题描述】:
我已经抓取了大量 (10GB) 的 PDF 并将它们转换为文本文件,但是由于原始 PDF 的格式,存在一个问题:
许多跨行的单词中都有一个破折号,人为地将单词分开,如下所示:
您可以看到这是因为原始 PDF 文件有中断:
在.txt 文件中“加入”与此模式匹配的每个单词实例的最简洁和最快的方法是什么?
也许某种正则表达式搜索,例如某种类型的[a-z]\-\s \w(单词字符后跟破折号后跟空格)会起作用吗?
或者某种sed 替换会更好吗?
目前,我正在尝试使用 sed 正则表达式,但我不确定如何翻译它以使用捕获组替换所选文本:
sed -n '\%\w\- [a-z]%p' Filename.txt
我的输入文本如下所示:
The dog rolled down the st- eep hill and pl- ayed outside.
输出将是:
The dog rolled down the steep hill and played outside.
理想情况下,该表达式也适用于由换行符分割的单词,如下所示:
The rule which provided for the consid-
eration of the resolution, was agreed to earlier by a
到这里:
The rule which provided for the consideration
of the resolution, was agreed to earlier by a
【问题讨论】:
-
可能使用 2 个捕获组并仅使用这 2 个组进行替换。
([a-z])-\r?\n(\w)regex101.com/r/IoGA1x/1 -
这似乎可行,如何实现捕获替换? (我在上面添加了我的尝试,仅供参考)。
-
刚刚将输入/输出文本添加到上述问题中。
标签: regex string bash sed data-cleaning