-1

I have 2 txt files.

The first txt file contains something like this: direction:left, move:right

The second txt file contains something like this: direction:right, move:right

Note: on both txt files, everything is on one line.

I want to be able to get the difference between those two txt files. So in the example above, it would return "right".


I tried using grep, comm, and diff. Those didn't work, because instead of printing the exact difference it just printed the different line, I just want the different phrase.

How do I do this in bash?

John Smith
  • 57
  • 3
  • 9
  • 1
    How is "a difference" defined, exactly? Why is it "direction:right", and not just "right"? Why is the output from the second file, and not showing the differing part from each file? – Benjamin W. Sep 02 '18 at 20:08
  • 1
    Please add the commands that you've tried for reference. – sripley Sep 02 '18 at 20:22
  • Please avoid *"Give me the codez"* questions. Instead show the script you are working on and state where the problem is. Also see [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/q/261592/608639) – jww Sep 02 '18 at 21:29
  • @BenjaminW. Sorry, that was a typo - it should only print right – John Smith Sep 02 '18 at 21:32

1 Answers1

-1

Use grep -F -x -v -f fileB fileA | cut -d':' -f2.

This works by using each line in fileB as a pattern (-f fileB) and treating it as a plain string to match (not a regular regex) (-F). You force the match to happen on the whole line (-x) and print out only the lines that don't match (-v). Therefore you are printing out the lines in fileA that don't contain the same data as any line in fileB.

Then cut -d':' -f2 splits the string with : as the delimiter and gets the second value.

sripley
  • 125
  • 5