Print lines from one file that are not contained in another file

前端 未结 4 1785
故里飘歌
故里飘歌 2020-12-09 03:37

I wish to print lines that are in one file but not in another file. However, neither files are sorted, and I need to retain the original order in both files.



        
相关标签:
4条回答
  • 2020-12-09 03:50

    comm <(sort a) <(sort b) -3 → Lines in file b that are not in file a

    0 讨论(0)
  • 2020-12-09 04:04

    In Perl, load file2 into a hash, then read through file1, outputing only lines that weren't in file2:

    use strict;
    use warnings;
    
    my %file2;
    open my $file2, '<', 'file2' or die "Couldn't open file2: $!";
    while ( my $line = <$file2> ) {
        ++$file2{$line};
    }
    
    open my $file1, '<', 'file1' or die "Couldn't open file1: $!";
    while ( my $line = <$file1> ) {
        print $line unless $file2{$line};
    }
    
    0 讨论(0)
  • 2020-12-09 04:05
    fgrep -x -f file2 -v file1
    

    -x match whole line

    -f FILE takes patterns from FILE

    -v inverts results (show non-matching)

    0 讨论(0)
  • 2020-12-09 04:05
    awk 'FNR==NR{a[$0];next} (!($0 in a))' file2 file1
    
    0 讨论(0)
提交回复
热议问题