简体   繁体   中英

Compare file1 and file2 but show only new lines which are not in file2

I am currently struggling with the task to compare two files. Both files have values which have differences and new lines. Example:

file1:

Germany=Munich
Swiss=Bern
Austria=Wien
Italy=Rom

file2:

Germany=Berlin
Swiss=Bern
Italy=Rom

The result of my action should be the following:

outputfile:

Austria=Wien

How can I achieve to get only lines to my output file which are not already in file2? I am not interested in differences of lines. Just a complete line which is missing.

I already experimented with diff and sdiff but without the desired results.

thanks

This should work:

awk -F= 'NR==FNR{a[$1]=$0;next}!($1 in a)' file2 file1
Austria=Wien

We read entire file2 first indexed at countries. We check if the country is not present in our file1 and print it. This won't give you results of lines which are in file2 but not in file1, but can be adjusted to give you that as well. I am not sure if that is your requirement. If it is then please update your question to reflect all your use-cases for more complete answer.

If you don't care about ordering, you can sort the files and then use join :

sort file1 > file1.srt
sort file2 > file2.srt
join -t'=' -v1 file1.srt file2.srt

The flags for join specify to use the equals sign as the field separator, include unpairable lines from file1.srt while suppressing the pairable lines from file1.srt.

This might work for you (GNU sed):

sed -r 's#([^=]*=).*#/^\1/d#' file2 | sed -f - file1

Use file2 as the basis for a sed script and run this sed script against file1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM