jk *_*ica 4 sed perl text-processing regular-expression
我有一个包含文本的源文本文件,其中一些单词的字母间隔就像这个问题中的单词“letterspaced”(即,单词的字母之间有一个空格字符。
如何使用 sed 撤消字母间距?
像这样的模式\{[A-Za-z] \}+[A-Za-z]
捕获一个字母间隔的单词,并将s/ //g
空格去掉,但是如何从一行文本中提取一个字母间隔的单词并撤消字母间隔而不损害文本其余部分中的合法空格字符?
你可以这样做:
sed -e's/ \([^ ][^ ]\)/\n\1/g' \
-e's/\([^ ][^ ]\) /\1\n/g' \
-e's/ //g;y/\n/ /
' <<\IN
I have a source text file containing text where
some words are l e t t e r s p a c e d
like the word "letterspaced" in this question
(i.e., there is a space character between the
letters of the word.
IN
Run Code Online (Sandbox Code Playgroud)
这个想法是首先找到前面或后面有两个或多个非空格字符的所有空格,并将它们作为换行符放在一边。接下来只需删除所有剩余的空格。最后,将所有换行符转换回空格。
这并不完美——如果没有包含一个完整的字典,你可能会使用最好的每个单词,这是某种启发式。不过这个还不错
此外,根据sed
您使用的内容,您可能还必须使用文字换行符代替n
I 在前两个替换语句中使用的内容。
但是,除了这个警告之外,这将适用于任何 POSIX 并且工作得非常快sed
。它不需要做任何代价高昂的前瞻或后视,因为它只是节省了不可能的事情,这意味着它可以处理单个地址中每个替换的所有模式空间。
I have a source text file containing text where some
words are letterspaced
like the word "letterspaced" in this question
(i.e., there is a space character between the
letters of the word.
Run Code Online (Sandbox Code Playgroud)