0

I use Ubuntu 18 and have little experience with Linux commands. I have two files:

file 1:

a,txt1,v1
b,txt2,v2
c,txt3,v1
d,txt4,v2

file2:

a,txt5,v2
b,txt6,v1
c,txt7,v1
d,txt8,v2

I want to print the 1st and 3rd column values where the the 1st column value is identical and the 3rd column value has changed from v1 to v2. So my expected output should be:

a

The second query is for the 3rd column values that has changed from v2 to v1. So my expected outcome:

b

closed as unclear what you're asking by αғsнιη, schily, Anthony G - justice for Monica, Jesse_b, slm Jul 13 '18 at 14:44

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

2
$ join -t, file1 file2 | awk -F, '$3 == "v1" && $5 == "v2" { print $1 }'
a

$ join -t, file1 file2 | awk -F, '$3 == "v2" && $5 == "v1" { print $1 }'
b

This is assuming that both files are sorted. If they are not, use

join -t, <( sort file1 ) <( sort file2 ) | awk ...as above...

The join command will produce

a,txt1,v1,txt5,v2
b,txt2,v2,txt6,v1
c,txt3,v1,txt7,v1
d,txt4,v2,txt8,v2

This is the result of a relational join between the two files using the first column as the join key. This lists the lines that have the same join key in both files and the other fields from each file.

The awk code looks at this and prints the first column only if column 3 and 5 have the values we are looking for.

Not the answer you're looking for? Browse other questions tagged or ask your own question.