我有一个文件1.txt
$cat 1.txt
page1
рage1
但:
$head -n1 1.txt | file -i -
/dev/stdin: text/plain; charset=us-ascii
$head -n2 1.txt | tail -n1 | file -i -
/dev/stdin: text/plain; charset=utf-8
字符串有不同的字符集.因为它我不能用我知道的方法得到唯一的字符串:
$cat 1.txt | sort | uniq -c | sort -rn
1 рage1
1 page1
那么,你能帮助我找到在我的情况下如何获得唯一字符串的方法吗?
附:首选解决方案只能使用linux命令行/ bash / awk.
但是如果你有另一种编程语言的解决方案,我也会喜欢它.
UPD. awk’!a [$0]’Input_file不起作用,pic:
$cat 1.txt
page1
рage1
$hd 1.txt
00000000 70 61 67 65 31 0a d1 80 61 67 65 31 0a |page1...age1.|
0000000d
正如对问题的评论中所指出的那样,第二个“рage1”确实与之前的“page1”不同,原因是:那不是拉丁语p,它是西里尔语р,所以唯一性过滤器应该将它们称为单独的,除非你事先规范化文本.
iconv不会在这里耍手段. uconv(例如在Debian / Ubuntu上安装icu-devtools)会让你接近,但它的transliteration mappings是基于语音而不是相似的字符,所以当我们音译这个例子时,西里尔语成为拉丁语r:
$uconv -x Cyrillic-Latin 1.txt
page1
rage1
另见these more complex uconv
commands,其结果相似.
uconv can also run the specified transliteration on the transcoded data,in which case transliteration will happen as an intermediate step,after the data have been transcoded to Unicode. The transliteration can be either a list of semicolon-separated transliterator names,or an arbitrarily complex set of rules in the ICU transliteration rules format.
这意味着有人可以使用“ICU音译规则格式”来指定相似的字符映射.当然,按照这个速度,你可以使用你想要的任何语言.
我也试过perl的Text::Unidecode,但它有自己的(类似的)问题:
$perl -Mutf8 -MText::Unidecode -pe '$_ = unidecode($_)' 1.txt
page1
NEURage1
在某些情况下,这可能会更好,但显然这不是其中之一.