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

查看:21
本文介绍了有没有办法在 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天全站免登陆