如何逐字比较两个文件并将不同的单词存储在结果输出文件中

时间:2019-06-11 19:08:14

标签: shell unix

假设有两个文件:

File1.txt

My name is Anamika.

File2.txt

My name is Anamitra.

我要存储结果文件:

Result.txt

Anamika                
Anamitra 

我使用腻子,所以不能使用wdiff,其他任何替代方法。

1 个答案:

答案 0 :(得分:0)

这不是我最出色的脚本,但是可以。其他人可能会想出一些更优雅的东西。

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    let setCenter = CGPoint(x: self.squareView.center.x + CGFloat(xPos), y: 
    self.squareView.center.y + CGFloat(yPos)

    arrayOfImages[i].frame = CGRect(x:CGFloat(xPos), y:CGFloat(yPos), width: 30, height: 30)
    arrayOfImages[i].center = setCenter
  }

这在#!/bin/bash if [ $# != 2 ] then echo "Arguments: file1 file2" exit 1 fi file1=$1 file2=$2 # Do this for both files for F in $file1 $file2 do if [ ! -f $F ] then echo "ERROR: $F does not exist." exit 2 else # Create a temporary file with every word from the file for w in $(cat $F) do echo $w >> ${F}.tmp done fi done # Compare the temporary files, since they are now 1 word per line # The egrep keeps only the lines diff starts with > or < # The awk keeps only the word (i.e. removes < or >) # The sed removes any character that is not alphanumeric. # Removes a . at the end for example diff ${file1}.tmp ${file2}.tmp | egrep -E "<|>" | awk '{print $2}' | sed 's/[^a-zA-Z0-9]//g' > Result.txt # Cleanup! rm -f ${file1}.tmp ${file2}.tmp 循环中使用了一个技巧。如果您使用for在文件上循环播放,则它将在每个单词上循环播放。并非像bash初学者这样的每一行都倾向于相信。在这里,实际上知道这是一件好事,因为它将文件转换为每行1个字。

例如:文件内容== for
    This is a sentence.循环完成后,临时文件将包含:

for

然后对文件运行This is a sentence. 很简单。 最后一个细节是,示例输出的末尾不包含diff,因此.命令仅保留字母数字字符。