Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 同时使用grep和awk_Bash_Awk_Grep - Fatal编程技术网

Bash 同时使用grep和awk

Bash 同时使用grep和awk,bash,awk,grep,Bash,Awk,Grep,我有一个文件(a.txt),其中有4列数字,另一个文件有3列数字(B.txt)。我需要解决以下问题: 查找A.txt中第三列的数字出现在B.txt第三列任何位置的所有行 假设我在一个目录中有许多像.txt这样的文件。我需要为那个目录中的每个文件运行这个 我该怎么做 下面是一个例子。创建以下文件并运行 awk -f c.awk B.txt A*.txt c.awk FNR==NR { s[$3] next } $3 in s { print FILENAME, $0

我有一个文件(a.txt),其中有4列数字,另一个文件有3列数字(B.txt)。我需要解决以下问题:

  • 查找A.txt中第三列的数字出现在B.txt第三列任何位置的所有行

  • 假设我在一个目录中有许多像.txt这样的文件。我需要为那个目录中的每个文件运行这个


  • 我该怎么做

    下面是一个例子。创建以下文件并运行

    awk -f c.awk B.txt A*.txt 
    
    c.awk

    FNR==NR {
        s[$3]
        next
    }
    
    $3 in s {
        print FILENAME, $0
    }
    
    A1.txt

    1 2 3
    1 2 6
    1 2 5
    
    1 2 3
    1 2 6
    1 2 5
    
    1 2 3
    1 2 5
    2 1 8
    
    A2.txt

    1 2 3
    1 2 6
    1 2 5
    
    1 2 3
    1 2 6
    1 2 5
    
    1 2 3
    1 2 5
    2 1 8
    
    B.txt

    1 2 3
    1 2 6
    1 2 5
    
    1 2 3
    1 2 6
    1 2 5
    
    1 2 3
    1 2 5
    2 1 8
    
    输出应为:

    A1.txt 1 2 3
    A1.txt 1 2 5
    A2.txt 1 2 3
    A2.txt 1 2 5
    

    你不应该看到有人同时使用
    grep
    awk
    ,因为无论
    grep
    能做什么,你都可以在
    awk
    中做:

    格雷普和奥克 仅使用Awk: 我必须把这件事说出来。现在谈谈你的问题

    Awk是一种编程语言,它假定一个循环遍历一组文件中的所有行。而且,你不想这样做。相反,您希望将
    B.txt
    视为一个特殊文件,并循环浏览其他文件。这通常需要Python或Perl之类的东西。(旧版本的BASH没有处理散列键数组,所以这些版本的BASH无法工作。)不过,看起来他找到了答案

    下面是一个Perl解决方案:

    use strict;
    use warnings;
    use feature qw(say);
    use autodie;
    
    my $b_file = shift;
    open my $b_fh, "<", $b_file;
    
    #
    # This tracks the values in "B"
    #
    my %valid_lines;
    while ( my $line = <$b_file> ) {
        chomp $line;
        my @array = split /\s+/, $line;
        $valid_lines{$array[2]} = 1;   #Third column
    }
    close $b_file;
    
    #
    # This handles the rest of the files
    #
    while ( my $line = <> ) {  # The rest of the files
       chomp $line;
       my @array = split /\s+/, $line;
       next unless exists $valid_lines{$array[2]};  # Next unless field #3 was in b.txt too
       say $line;
    }
    
    使用严格;
    使用警告;
    使用特征qw(例如);
    使用自动模具;
    我的$b_文件=移位;
    
    打开我的$b_fh,“A
    while
    awk
    一起循环应该足够了。你尝试过什么?你有没有任何样本输入和期望的输出?请粘贴一些示例…听起来awk可以处理。@fedorqui,甚至不需要一段时间循环这个(1)对于
    join
    …以及
    for
    循环遍历单个文件(2)来说,这听起来是个不错的工作这很好。但是,如果
    B.txt
    中有一行不在其他文件中,会发生什么?我将这一行添加到了B.txt“2 1 8”。它不会更改输出。好的,我明白了。FNR仅用于原始文件。如果该行在
    B.txt
    中,则只将内容放入
    s
    中。我认为下半部分仅为e一旦你用完了
    B.txt
    。re:
    你不应该看到有人同时使用grep和awk…
    我在
    /var/log
    中有一系列
    syslog
    文件(一些压缩文件)。我需要匹配一个字符串
    电压
    ,作为需要进一步处理的标志,但是这个字符串并不总是在同一个字段中。
    zgrep
    awk
    让我觉得这是一个合理的方法。如果我可以用一个简单的
    grep
    操作完成一个复杂的
    awk
    操作,那么为什么不呢?