Skip to content
Advertisement

Merging two files in bash with a twist in shell linux

The following question is somehow tricky but seemingly simple , i need to use bash

let us suppose i have 2 text files, the first on is

FirstFile.txt

0 1
0 2
1 1
1 2
2 0

SecondFile.txt

0 1
0 2
0 3
0 4
0 5
1 0
1 1
1 2 
1 3
1 4
1 5
2 1 
2 2 
2 3
2 4 
2 5

I want to be able to create a new Thirdfile.txt that contains that values that are not in file A , meaning if there is a common variable with file A i want it to be removed. knowing that 2 0 and 0 2 are the same …

Can you help me out ?

Advertisement

Answer

Using awk, you can rearrange the columns so that the lower number is always first. When reading the first file, save them as keys in an associative array. When reading the second file, test if they’re not found in the array.

awk '{if ($1 <= $2) { a = $1; b = $2; } else { a = $2; b = $1 } }
     FNR==NR { arr[a, b] = 1; next; }
     !arr[a, b]' FirstFile.txt SecondFile.txt > ThirdFile.txt

Results:

0 3
0 4
0 5
1 3
1 4
1 5
2 2 
2 3
2 4 
2 5
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement