Monday, 15 August 2011

bash - How to remove common lines between two files without sorting? -



bash - How to remove common lines between two files without sorting? -

this question has reply here:

compare 2 files , remove lines in file2 when match values found in file1 4 answers

i have 2 files not sortered have lines in common.

file1.txt

z b h l

file2.txt

s l w q

the way i'm using remove mutual lines 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

output:

b h z

the problem want maintain order of file1.txt, mean:

desired output:

z b h

one solution tought doing loop read lines of file2.txt and:

sed -i '/^${line_file2}$/d' file1.txt

but if files big performance may suck.

do idea? do have alternative it?

grep or awk:

awk 'nr==fnr{a[$0]=1;next}!a[$0]' file2 file1

bash sorting optimization sed comm

No comments:

Post a Comment