有没有办法在Unix中删除文件中的重复标题? [英] Is there way to delete duplicate header in a file in Unix?

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

问题描述

如何从文件中删除多个标题?在如何在Unix中删除文件中的重复行?.

How can I delete multiple headers from a file? I tried to use the below code after finding it from How can I delete duplicate lines in a file in Unix?.

awk '!x[$0]++' file.txt

它将删除文件中所有重复的记录.但就我而言,我只需要删除标头重复项,而不是文件中的重复记录.例如,我有一个包含以下数据的文件:

It is deleting all the duplicate records in the file. But in my case, I just need the header duplicates to be removed, not the duplicate records in the file. For example, I have a file with the below data:

column1, column2, column3, column4, column5
value11, value12, value13, value14, value14
value21, value22, value23, value24, value25
value31, value32, value33, value34, value35
value41, value42, value43, value44, value45
value51, value52, value53, value54, value55
value21, value22, value23, value24, value25
column1, column2, column3, column4, column5
value11, value12, value13, value14, value14
value21, value22, value23, value24, value25
column1, column2, column3, column4, column5
column1, column2, column3, column4, column5

我期望输出如下:

column1, column2, column3, column4, column5
value11, value12, value13, value14, value14
value21, value22, value23, value24, value25
value31, value32, value33, value34, value35
value41, value42, value43, value44, value45
value51, value52, value53, value54, value55
value21, value22, value23, value24, value25
value11, value12, value13, value14, value14
value21, value22, value23, value24, value25

推荐答案

如果您知道第一行包含标题,则只需删除该标题的所有其他实例即可.

If you know that the first line contains the header, just delete all other instances of that.

awk 'FNR==1 { header = $0; print }
     $0 != header' file

如果这行不通,请告诉我们如何识别标题行.如果只是静态字符串grep -vF 'that string',或者与特定正则表达式匹配,则为grep -v 'that regex'.

If that won't work, please tell us how we can identify a header line. If it's just a static string, grep -vF 'that string' or if it matches a particular regex, grep -v 'that regex'.

这篇关于有没有办法在Unix中删除文件中的重复标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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