unix工具从文件中删除重复行 [英] unix tool to remove duplicate lines from a file

查看:266
本文介绍了unix工具从文件中删除重复行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个工具可以生成测试并预测输出。这个想法是,如果我有一个失败,我可以将预测与实际输出进行比较,看看他们在哪里分歧。问题是实际输出包含一些行两次,这会混淆 diff 。我想删除重复的,所以我可以很容易地比较。基本上,像 sort -u ,但没有排序。

I have a tool that generates tests and predicts the output. The idea is that if I have a failure I can compare the prediction to the actual output and see where they diverged. The problem is the actual output contains some lines twice, which confuses diff. I want to remove the duplicates, so that I can compare them easily. Basically, something like sort -u but without the sorting.

有任何unix命令行工具, ?

Is there any unix commandline tool that can do this?

推荐答案

uniq(1)

概要


uniq [OPTION] ... [INPUT [OUTPUT]]

uniq [OPTION]... [INPUT [OUTPUT]]

DESCRIPTION

DESCRIPTION


丢弃来自INPUT(或标准输入)的连续相同行中的所有行,写入OUTPUT(或标准输出)。

Discard all but one of successive identical lines from INPUT (or standard input), writing to OUTPUT (or standard output).

或者,如果你想删除不相邻的重复行,这个perl片段将会这样做:

Or, if you want to remove non-adjacent duplicate lines as well, this fragment of perl will do it:

while(<>) {
    print $_ if (!$seen{$_});
    $seen{$_}=1;
}

这篇关于unix工具从文件中删除重复行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆