我需要从文件中删除所有重复行,但忽略这些字符的所有外观:
(),、“”。!?#例如,这两行将被认为是重复的,因此其中一行将被删除:
“This is a line。“
This is a line同样,这三行将被视为重复,只有一条保留:
This is another line、 with more words。
“This is another line with more words。”
This is another line! with more words!如何在忽略某些字符的同时删除文件中的所有重复行?
发布于 2014-02-21 11:50:25
从您的示例中,您可以删除您的符号,然后删除您的副本。
例如:
$ cat foo
«This is a line¡»
This is another line! with more words¡
Similarly, these three lines would be considered duplicates, and only one would remain:
This is a line
This is another line, with more words!
This is another line with more words
$ tr --delete '¡!«»,' < foo | awk '!a[$0]++'
This is a line
This is another line with more words
Similarly these three lines would be considered duplicates and only one would remain:
$似乎能做好这份工作。
编辑:
从你的问题来看,那些符号/标点符号似乎无关紧要。你应该精确点。
我没有时间写这篇文章,但我认为最简单的方法应该是解析您的文件并维护一个已经打印过的行数组:
for each line:
cleanedLine = stripFromSymbol(line)
if cleanedLine not in AlreadyPrinted:
AlreadyPrinted.push(cleanedLine)
print line发布于 2014-02-21 18:26:23
这是一种方法。您将它们收集到按规范化版本键键的数组中。在这里规范化意味着移除所有你不想要的字符,并压缩空间。然后选择最短的版本打印/保存。那种需要保留的启发式方法并没有被真正指定为适合品尝的季节。代码对于生产来说有点简洁,所以为了清晰起见,您可能会充实它。
use utf8;
use strictures;
use open qw/ :std :utf8 /;
my %tree;
while (my $original = <DATA>) {
chomp $original;
( my $normalized = $original ) =~ tr/ (),、“”。!?#/ /sd;
push @{$tree{$normalized}}, $original;
#print "O:",$original, $/;
#print "N:",$normalized, $/;
}
@{$_} = sort { length $a <=> length $b } @{$_} for values %tree;
print $_->[0], $/ for values %tree;
__DATA__
“This is a line。“
This is a line
This is a line
This is another line、 with more words。
This is another line with more words
This is another line! with more words!收益率-
This is another line with more words
This is a linehttps://stackoverflow.com/questions/21933234
复制相似问题