Linux 将txt文件中的每一行与每一行中的另一个文本文件进行比较

Linux 将txt文件中的每一行与每一行中的另一个文本文件进行比较,linux,bash,shell,Linux,Bash,Shell,这几天我在shell脚本方面遇到了一些困难。 做了一些测试,但仍然无法正常工作。 我试图将文本文件中的每一行与另一个文本文件进行比较。这样做的目的是查看文件2中是否有行 有人能看出我的剧本有什么问题吗 谢谢 #!/bin/bash FILE1='/filePath/file.txt' FILE2='/filePath/file2.txt' for line in $FILE1 do for line2 in $FILE2 do if $line

这几天我在shell脚本方面遇到了一些困难。 做了一些测试,但仍然无法正常工作。 我试图将文本文件中的每一行与另一个文本文件进行比较。这样做的目的是查看文件2中是否有行

有人能看出我的剧本有什么问题吗

谢谢

#!/bin/bash
  FILE1='/filePath/file.txt'
  FILE2='/filePath/file2.txt'
  for line in $FILE1 
  do
    for line2 in $FILE2
    do
        if  $line != $line2
            then
            echo -e /> diffsScr.txt
        fi
    done 
  done

您可以使用以下命令实现此结果:

fgrep -v -x -f '/filePath/file2.txt' '/filePath/file1.txt'

您可以使用以下命令实现此结果:

fgrep -v -x -f '/filePath/file2.txt' '/filePath/file1.txt'

您可以使用以下命令实现此结果:

fgrep -v -x -f '/filePath/file2.txt' '/filePath/file1.txt'

您可以使用以下命令实现此结果:

fgrep -v -x -f '/filePath/file2.txt' '/filePath/file1.txt'

如果文件已排序,则可以使用comm比较文件

comm -23 file1 file2  
通信说明:

   Compare sorted files FILE1 and FILE2 line by line.

   With  no  options,  produce  three-column  output.  Column one contains
   lines unique to FILE1, column two contains lines unique to  FILE2,  and
   column three contains lines common to both files.

   -1     suppress column 1 (lines unique to FILE1)

   -2     suppress column 2 (lines unique to FILE2)

   -3     suppress column 3 (lines that appear in both files)

如果文件已排序,则可以使用comm比较文件

comm -23 file1 file2  
通信说明:

   Compare sorted files FILE1 and FILE2 line by line.

   With  no  options,  produce  three-column  output.  Column one contains
   lines unique to FILE1, column two contains lines unique to  FILE2,  and
   column three contains lines common to both files.

   -1     suppress column 1 (lines unique to FILE1)

   -2     suppress column 2 (lines unique to FILE2)

   -3     suppress column 3 (lines that appear in both files)

如果文件已排序,则可以使用comm比较文件

comm -23 file1 file2  
通信说明:

   Compare sorted files FILE1 and FILE2 line by line.

   With  no  options,  produce  three-column  output.  Column one contains
   lines unique to FILE1, column two contains lines unique to  FILE2,  and
   column three contains lines common to both files.

   -1     suppress column 1 (lines unique to FILE1)

   -2     suppress column 2 (lines unique to FILE2)

   -3     suppress column 3 (lines that appear in both files)

如果文件已排序,则可以使用comm比较文件

comm -23 file1 file2  
通信说明:

   Compare sorted files FILE1 and FILE2 line by line.

   With  no  options,  produce  three-column  output.  Column one contains
   lines unique to FILE1, column two contains lines unique to  FILE2,  and
   column three contains lines common to both files.

   -1     suppress column 1 (lines unique to FILE1)

   -2     suppress column 2 (lines unique to FILE2)

   -3     suppress column 3 (lines that appear in both files)
这将读取文件
input1
,并构建一个数组。然后它通过
input2
并打印未出现在
input1
中的每一行。如果要添加行号,请执行以下操作:

awk 'FNR==NR{f[$0]+=1; next} !($0 in f) { print FNR, $0}' input1 input2
这种方法的一大优点是它具有良好的可扩展性。您的方法是O(n*m),其中n和m是文件中的行数,但预读入这样的数组会为您提供一个O(n+m)的解决方案。换句话说,每个文件只读取一次

这将读取文件
input1
,并构建一个数组。然后它通过
input2
并打印未出现在
input1
中的每一行。如果要添加行号,请执行以下操作:

awk 'FNR==NR{f[$0]+=1; next} !($0 in f) { print FNR, $0}' input1 input2
这种方法的一大优点是它具有良好的可扩展性。您的方法是O(n*m),其中n和m是文件中的行数,但预读入这样的数组会为您提供一个O(n+m)的解决方案。换句话说,每个文件只读取一次

这将读取文件
input1
,并构建一个数组。然后它通过
input2
并打印未出现在
input1
中的每一行。如果要添加行号,请执行以下操作:

awk 'FNR==NR{f[$0]+=1; next} !($0 in f) { print FNR, $0}' input1 input2
这种方法的一大优点是它具有良好的可扩展性。您的方法是O(n*m),其中n和m是文件中的行数,但预读入这样的数组会为您提供一个O(n+m)的解决方案。换句话说,每个文件只读取一次

这将读取文件
input1
,并构建一个数组。然后它通过
input2
并打印未出现在
input1
中的每一行。如果要添加行号,请执行以下操作:

awk 'FNR==NR{f[$0]+=1; next} !($0 in f) { print FNR, $0}' input1 input2

这种方法的一大优点是它具有良好的可扩展性。您的方法是O(n*m),其中n和m是文件中的行数,但预读入这样的数组会为您提供一个O(n+m)的解决方案。换句话说,每个文件只读取一次。

为什么不使用命令
diff
?另外,最好显示输入/期望的输出。因为他没有给我正确的输出,已经尝试过了。他仍然给我两个文件中的行。不是我想要的:/Note
mandiff
有很多选择。读一读它,因为你可能会找到一个帮助你!(只看逻辑)循环的设置方式是将file1中的每一行与file2中的所有行进行比较。听起来你想逐行比较。这正是我想要的。要知道第二个文件中是否有行,为什么不使用命令
diff
?另外,最好显示输入/期望的输出。因为他没有给我正确的输出,已经尝试过了。他仍然给我两个文件中的行。不是我想要的:/Note
mandiff
有很多选择。读一读它,因为你可能会找到一个帮助你!(只看逻辑)循环的设置方式是将file1中的每一行与file2中的所有行进行比较。听起来你想逐行比较。这正是我想要的。要知道第二个文件中是否有行,为什么不使用命令
diff
?另外,最好显示输入/期望的输出。因为他没有给我正确的输出,已经尝试过了。他仍然给我两个文件中的行。不是我想要的:/Note
mandiff
有很多选择。读一读它,因为你可能会找到一个帮助你!(只看逻辑)循环的设置方式是将file1中的每一行与file2中的所有行进行比较。听起来你想逐行比较。这正是我想要的。要知道第二个文件中是否有行,为什么不使用命令
diff
?另外,最好显示输入/期望的输出。因为他没有给我正确的输出,已经尝试过了。他仍然给我两个文件中的行。不是我想要的:/Note
mandiff
有很多选择。读一读它,因为你可能会找到一个帮助你!(只看逻辑)循环的设置方式是将file1中的每一行与file2中的所有行进行比较。听起来你想逐行比较。这正是我想要的。要知道第二个文件中是否有行“-v”和“-f”做什么?“-v”和“-f”做什么?“-v”和“-f”做什么?“-v”和“-f”做什么?