如何在不排序的情况下删除两个文件之间的公共行? [英] How to remove common lines between two files without sorting?
本文介绍了如何在不排序的情况下删除两个文件之间的公共行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个未排序的文件,它们有一些共同点.
I have two files not sortered which have some lines in common.
file1.txt
file1.txt
Z
B
A
H
L
file2.txt
file2.txt
S
L
W
Q
A
我用于删除常见行的方法如下:
The way I'm using to remove common lines is the following:
sort -u file1.txt > file1_sorted.txt
sort -u file2.txt > file2_sorted.txt
comm -23 file1_sorted.txt file2_sorted.txt > file_final.txt
输出:
B
H
Z
问题是我想保持file1.txt的顺序,我的意思是:
The problem is that I want to keep the order of file1.txt, I mean:
所需的输出:
Z
B
H
我最坚决的一种解决方案是循环读取file2.txt的所有行,以及:
One solution I tought is doing a loop to read all the lines of file2.txt and:
sed -i '/^${line_file2}$/d' file1.txt
但是,如果文件很大,性能可能会降低.
But if files are big the performance may suck.
- 你喜欢我的主意吗?
- 您有其他选择吗?
推荐答案
grep或awk:
awk 'NR==FNR{a[$0]=1;next}!a[$0]' file2 file1
这篇关于如何在不排序的情况下删除两个文件之间的公共行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文