如何删除包含专用区域字符的行



给定一个文件,其中字符位于私人使用区域,如下所示:

$ cat textfile.txt | less
10 翴 30 <U+E4D1>       ten-thirty in ... three ... two ... one .
- 10 翴 45だи<U+E145>砆 秂 <U+E18E>     it 's a slam-dunk .
<U+E707> 10 翴 <U+E6C4>ㄓ ?     so you will be home by 10:00 ?
10 翴 牧 よ<U+E6BC>ㄓ<U+E5EC>   bogey at 10 o'clock .
- 10 翴 牧 よ<U+E6BC>い盠       - ten o'clock , lieutenant , 10 o'clock !
10 翴 牧 よ<U+E6BC>绰玭 i see it , 8 o'clock , heading south !
10 翴 筁<U+E5EC>        it 's past 10:00 .
<U+E80B>ぱ 10 翴 非<U+E1A0>筁ㄓ be here tomorrow , 10:00 sharp .
- 10 , 老搭档 有 人 开枪 , 疑犯 拒捕  shots firing . suspect 's fleeing .
- 1 -0 而已     - only 1-0 .
- 1 -0 而已     - only 1-0 .

如果有任何字符超出unicode字节点,我如何删除一行?

我试过了:

# ord(u'uE000') == 57344
for line in open('test.txt'):
    if any(ord(i) >57344 for i in line):
        pass
    else:
        print (line)

但是我似乎无法摆脱包含PUA字符的行。

我如何在unix bash中使用sed/awk或其他东西而不是使用Python实现相同的功能?

请注意,我仍然希望保留有效的unicode行,而不仅仅保留带有ascii字符的行。例:我想保留最后第三行加上汉字和"……"枪声四起。(由于某些原因,我无法在问题中键入中文部分,因为SO显示中文字符错误)

您检查字符是否属于私人使用区域的标准(ord(i) > 57344)不正确:

目前,定义了三个私人使用区域:一个在基本多语面(U+E000–U+F8FF),一个在几乎覆盖15和16面(U+F0000–U+FFFFD, U+100000–U+10FFFD)

下面是固定的Python 3代码:

pua_ranges = ( (0xE000, 0xF8FF), (0xF0000, 0xFFFFD), (0x100000, 0x10FFFD) )
def is_pua_codepoint(c):
    return any(a <= c <= b for (a,b) in pua_ranges)
for line in open('test.txt', 'r'):
    if any(is_pua_codepoint(ord(i)) for i in line):
        pass
    else:
        print(line)

这个grep命令将匹配U+ E000-U +F8FF范围内任何不包含PUA字符的行:

grep -Pv "[xe0x00-xf8xff]"

最新更新