Need help on below req
Compare two files and send difference of file to other file
File2 is static which never changes
ex:
File1
A.txt
B.ttx
C.txt
E.txt
File2
A.txt
B.txt
C.txt
D.txt
Req:
i need to get two output files
1) File 2 has "D.txt" where File1 does not have "D.txt" then I should get "D.txt" to new files > file3.txt
2)In file1 E.txt is total new file I should get "E.txt" has output to new file > File4.txt
Hello,
Following commands may help you, also please always use code tags while posting commands and codes.
awk 'NR==FNR{a[$1]=$1;next} !($1 in a){print $1 > "File2_to_File1_differance"}' file1_details_check1211231 file_details_check12111232
awk 'NR==FNR{a[$1]=$1;next} !($1 in a){print $1 > "File1_to_File2_differance"}' file_details_check12111232 file1_details_check1211231
It will create File2_to_File1_differance and File1_to_File2_differance 2 output files for same. Where file1_details_check1211231 is file1 and file_details_check12111232 is file2.
Thanks,
R. Singh
Thanks for reply
its not working!
file2_file1 diff -> getting out all value in file2, same as for file1_file1diff
our env is linux
Sathish,
Could you please use the following commands and output files will have as follows.
awk 'NR==FNR{a[$1]=$1;next} !($1 in a){print $1 > "File2_to_File1_differance"}' file1_details_check1211231 file_details_check12111232
cat File2_to_File1_differance
D.txt
awk 'NR==FNR{a[$1]=$1;next} !($1 in a){print $1 > "File1_to_File2_differance"}' file_details_check12111232 file1_details_check1211231
cat File1_to_File2_differance
E.txt
Also I am concendring in file 1 b.ttx
is a typo. So I have taken it as b.txt
.
Thanks,
R. Singh
Although I find your file names very strange (some with an upper case F
, some with a lower case f
, some ending with .txt
, and some not having any filename extension), the following single awk
command seems to do what you want:
awk '
FNR == NR {
a[$1]
next
}
{ if($1 in a)
delete a[$1]
else print $1 > "file3.txt"
}
END { for(i in a)
print i > "File4.txt"
}' File1 File2
With File1
and File2
as specified in post #1 in this thread, it writes the following to file3.txt
:
B.txt
D.txt
and writes the following to File4.txt
:
B.ttx
E.txt
If you want to try this on a Solaris/SunOS system, change awk
to /usr/xpg4/bin/awk
, /usr/xpg6/bin/awk
, or nawk
.
Note that unlike RavinderSingh13's awk
scripts, the order of output lines in File4.txt
from the above script is random rather than in the order in which they appear in the input file. The output in file3.txt
should be the same either way.