linux - Comparing each line in txt file to another text file in each line -


i'm having difficulties shell script these days. done test , still can't make work. i'm trying compare each line in text file text file. idea see if line not in file2.

could see wrong on script ?

thanks!!

#!/bin/bash   file1='/filepath/file.txt'   file2='/filepath/file2.txt'   line in $file1        line2 in $file2             if  $line != $line2                         echo -e /> diffsscr.txt         fi     done    done 

awk 'fnr==nr{f[$0]+=1; next} !($0 in f)' input1 input2 

this reads through file input1 , builds array. goes through input2 , prints each line did not appear in input1. if want add line numbers:

awk 'fnr==nr{f[$0]+=1; next} !($0 in f) { print fnr, $0}' input1 input2 

one big advantage of approach scales well. approach o(n*m) n , m number of lines in files, pre-reading array gives solution o(n+m). in other words, read through each file once.


Comments