本文介绍了删除具有相似前缀的重复行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要删除前缀重复的文件中的相似行,并保留唯一的行.

I need to remove similar lines in a file which has duplicate prefix and keep the unique ones.

从这开始,

abc/def/ghi/
abc/def/ghi/jkl/one/
abc/def/ghi/jkl/two/
123/456/
123/456/789/
xyz/

对此

abc/def/ghi/jkl/one/
abc/def/ghi/jkl/two/
123/456/789/
xyz/

赞赏任何建议,

推荐答案

一种快速而肮脏的方法如下:

A quick and dirty way of doing it is the following:

$ while read elem; do echo -n "$elem " ; grep $elem file| wc -l; done <file | awk '$2==1{print $1}'
abc/def/ghi/jkl/one/
abc/def/ghi/jkl/two/
123/456/789/
xyz/

在其中读取输入文件并打印每个元素及其在文件中出现的时间,然后使用awk仅打印仅出现1次的行.

where you read the input file and print each elements and the number of time it appears in the file, then with awk you print only the lines where it appears only 1 time.

这篇关于删除具有相似前缀的重复行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-25 00:12