How to compare 2 files having random numbers in non sequential order?
Here's how to do what you want just using awk:
$ awk '{$0+=0} NR==FNR{a[$0];next} !($0 in a)' compare1.txt compare2.txt
12
90
$ awk '{$0+=0} NR==FNR{a[$0];next} !($0 in a)' compare2.txt compare1.txt
11
91
but this is the job that comm
exists to do so here's how you could use that to get all differences and common lines at once. In the following output col1 is compare1.txt only, col2 is compare2.txt only, col3 is common between both files:
$ comm <(awk '{print $0+0}' compare1.txt | sort) <(awk '{print $0+0}' compare2.txt | sort)
11
12
13
14
3
57
889
90
91
or to get each result individually:
$ comm -23 <(awk '{print $0+0}' compare1.txt | sort) <(awk '{print $0+0}' compare2.txt | sort)
11
91
$ comm -13 <(awk '{print $0+0}' compare1.txt | sort) <(awk '{print $0+0}' compare2.txt | sort)
12
90
$ comm -12 <(awk '{print $0+0}' compare1.txt | sort) <(awk '{print $0+0}' compare2.txt | sort)
13
14
3
57
889
Could you please try following, written and tested with shown samples in GNU awk
.
awk '
{
$0=$0+0
}
FNR==NR{
a[$0]
next
}
($0 in a){
b[$0]
next
}
{ print }
END{
for(j in a){
if(!(j in b)){ print j }
}
}
' compare1.txt compare2.txt
Explanation: Adding detailed explanation for above.
awk ' ##Starting awk program from here.
{
$0=$0+0 ##Adding 0 will remove extra zeros from current line,considering that your file doesn't have float values.
}
FNR==NR{ ##Checking condition FNR==NR which will be TRUE when 1st Input_file is being read.
a[$0] ##Creating array a with index of current line here.
next ##next will skip all further statements from here.
}
($0 in a){ ##Checking condition if current line is present in a then do following.
b[$0] ##Creating array b with index of current line.
next ##next will skip all further statements from here.
}
{ print } ##will print current line from 2nd Input_file here.
END{ ##Starting END block of this code from here.
for(j in a){ ##Traversing through array a here.
if(!(j in b)){ print j } ##Checking condition if current index value is NOT present in b then print that index.
}
}
' compare1.txt compare2.txt ##Mentioning Input_file names here.