0

I got two almost identical files, same amount of lines and it's a code. I'm trying to create a file of the common lines between these two files and also have blank lines where the lines are different. I tried using comm, and it works good but doesn't provide me the blank lines I need on the bad lines, it just eliminates the lines and the common file is shorter(line count).

This is what I tried:

comm -1 -2 file1 file2
codeforester
  • 39,467
  • 16
  • 112
  • 140
lyoko
  • 429
  • 3
  • 4

1 Answers1

1

comm needs sorted files. So, you could use command substitution like this:

comm -12 <(sort file1) <(sort file2)

If you want to skip blank lines (spaces), then:

comm -12 <(grep -Ev '^[ ]+$' file1 | sort) <(grep -Ev '^[ ]+$' file2 | sort)

To skip blank lines that have spaces or tabs:

comm -12 <(grep -Ev $'^[ \t]+$' file1 | sort) <(grep -Ev $'^[ \t]+$' file2 | sort)
codeforester
  • 39,467
  • 16
  • 112
  • 140