2

我需要逐行比较两个文件,排除公共行,然后使用shell命令输出所有不同的文件。

例子:

文件 1:

5
124
346
12
65
8
78

文件 2:

10
23
129494
5
493
124
4999
346

输出:

12
65
8
78
129494
943
4999

谢谢

好的,让我添加一些细节:我有一些文件,包括 IP 对。

例子:

文件 1:

55.4.56.11 10.22.123.43 10.22.123.43 147.34.123.43 147.34.23.2 23.124.251.1

文件 2:

123.4.23.89 121.45.60.0 121.45.60.0 0.0.0.0 120.3.2.129 45.55.68.09 45.55.68.09 66.67.23.111 55.4.56.11 10.22.123.43

所以在这个例子中,我需要两个文件的每一行作为输出,除了:55.4.56.11 10.22.123.43

这意味着我不能使用数字比较。此外,整理文件也无济于事,因为可能有不同的行数。我需要两个文件的“全局”比较。如果你们需要更多详细信息,我很乐意进一步编辑我的帖子。感谢您的时间。(我不能让第二个例子看起来像第一个,我不知道为什么,但假设每两个 IPS 后有一个换行符)

4

4 回答 4

2

使用 grep:

grep -xv -f f2 f1 && grep -xv -f f1 f2
12
65
8
78
10
23
129494
493
4999
于 2014-03-21T18:30:42.313 回答
1

这是comm 命令和进程替换的理想选择。

comm -3 <(sort -un f1) <(sort -un f2)

如果您想要一个平面输出文件,您将需要通过 comm 命令修剪前导空格输出

comm -3 <(sort -un f1) <(sort -un f2) | tr -d '\t'
于 2014-03-21T18:36:13.837 回答
0

使用 java :

   import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.HashSet;
    import java.util.Set;

    public class ComapreServerPackage {

        public static void main(String[] args) throws IOException {
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

            System.out.println("Path of source file : ");
            String sourceFilePath = reader.readLine();

            System.out.println("Path of target file : ");
            String targetFilePath = reader.readLine();

            File sourceFile = new File(sourceFilePath);
            File taregtFile = new File(targetFilePath);

            BufferedReader fileReader = new BufferedReader(new FileReader(sourceFile));

            String readLine = "";

            System.out.println("Comparing ...");


            Set <String> sourceSet = new HashSet();

            // file source set 
            while ((readLine = fileReader.readLine()) != null) {
                sourceSet.add(readLine);
            }

            fileReader.close();
            fileReader =  new BufferedReader(new FileReader(taregtFile));

            System.out.println("Lines not available in source file are : ");
            // file source set 
            while ((readLine = fileReader.readLine()) != null) {
                if(!sourceSet.contains(readLine))
                    System.out.println(readLine);
            }

            System.out.println("*************** end *********************");
            reader.close();
            fileReader.close();

        }

    }
于 2017-06-30T11:14:06.590 回答
0

替代解决方案awk

awk 'BEGIN { while ( getline < "f2.txt" > 0 ) _[$1]++ }{if (!($1 in _)) {print $1}}' f1.txt && awk 'BEGIN { while ( getline < "f1.txt" > 0 ) _[$1]++ }{if (!($1 in _)) {print $1}}' f2.txt

细节:

awk 'Command1 Command2' fileName

Command1:BEGIN { while ( getline < "f2.txt" > 0 ) _[$1]++ } 读取每一行f2.txt并存储到一个数据结构中。

Command2:{if (!($1 in _)) {print $1}}对于 awk 处理的文件中的每一行f1.txt,打印不存在的行f2.txt

fileName:f1.txtawk 处理的文件。

这应用于每个文件,我们得到了结果。

于 2019-08-24T09:59:57.740 回答