Hello. There are two files ( file1.txt and file2.txt ), their contents are as follows, file1:

 abcd 

file2:

 abxz 

I am writing a script to display the difference in the third file from the new line:

grep -F -v -f file2.txt file1.txt >> diff.txt

But the output is obtained only "cd", i.e. in one line.
I would be grateful for the help!

UPDATE: The problem was solved by adding | awk '{sub (/ $ /, "\ r"); print}'

  • one
    And what is the "difference"? and why grep itself, there is a diff utility for finding differences in files - Mike
  • In this case, the difference is that there is only in the first file - Artemiy Ivanov
  • and files file2.txt file1.txt in which OS are created? - Senior Pomidor
  • just add the extra character grep -F -v -f file2.txt file1.txt | awk '{print $0,"\n"}' >> diff.txt grep -F -v -f file2.txt file1.txt | awk '{print $0,"\n"}' >> diff.txt - Senior Pomidor
  • @SeniorPomidor files created in Windows. With the addition of awk, spaces are simply added to the output, i.e. "cd" - Artemiy Ivanov

1 answer 1

the problem is that the line ending character is different in different OS. therefore, windows will show you characters from the new line, and for Unix they will be different. Because of this, you don’t see newline characters in linux. This can be solved by adding the characters \r\n

 grep -F -v -f file2.txt file1.txt | awk '{print $0,"\r\n"}' >> diff.txt 
  • Thank! And what is the fundamental difference between '{print $ 0, "\ r \ n"}' and '{sub (/ $ /, "\ r"); print}'? - Artemiy Ivanov
  • sub converts Windows / DOS characters (CRLF) to Unix (LF). your option is better than mine. but there will be problems if this file is then opened in windows. then there will be one long line without hyphenation - Senior Pomidor