【问题标题】:compare two columns from different tables and append values for matching rows to first file比较来自不同表的两列并将匹配行的值附加到第一个文件
【发布时间】:2015-10-11 05:20:20
【问题描述】:

我正在尝试将纯文本密码添加到导出的用户表中的相应行。我有两个文件:

用户表

    barry  hash1
    fred   hash2
    mary   hash3
    rich   hash2

破解通行证

    ptpass1 hash1
    ptpass2 hash2

我希望输出看起来像:

组合表

    barry  hash1  ptpass1
    fred   hash2  ptpass2
    mary   hash3
    rich   hash2  ptpass2

我已经根据我找到的另一个条目尝试了以下代码,但这只会打印出匹配的行,而不是全部:

$ awk 'NR==FNR{a[$2]=$0; next} a[$2]>0{print a[$2],$1}' usertable crackpasstable > combinedtable

我想尝试打印出用户表中的所有行(希望按照原始顺序),如果我能够破解它们,只需在新列中添加匹配的密码。

谢谢

【问题讨论】:

    标签: awk


    【解决方案1】:

    pr.awk:

    # Usage:
    # awk -f pr.awk crackpasstable usertable > combintedtable
    
    BEGIN {
       ct = ARGV[1]
       ARGV[1] = "" # read ARGV[1] file implicitly, ARGV[2] file
                    # explicitly
       while (getline < ct > 0) {
           p = $1
           h = $2
           phash[h] = p
        }
    }
    
    {
       h = $2
       if (h in phash)
           print $0 "  " phash[h]
        else
           print
    }
    

    【讨论】:

      【解决方案2】:

      你几乎是对的。读入crackpasstable,然后扫描usertable并从加载的文件中添加pass

      # read in the first file
      FNR == NR {
          hashes [$2] = $1 # hopefully, there are no repeating hash values
          next
      }
      
      # scan all other files
      $2 in hashes {
          $0 = $0 "\t" hashes[$2]
      }
      
      1 # or {print} if you prefer to be explicit
      

      用法:

      > ThisScript crackpasstable usertable
      

      【讨论】:

        【解决方案3】:

        或

        awk 'NR==FNR {a[$2]=$1;next} {print ($2 in a) ? $0"\t"a[$2] : $0}' crackpasstable usertable
        

        【讨论】:

          猜你喜欢
          • 2015-04-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-02-11
          • 2014-02-27
          • 2019-12-16
          相关资源
          最近更新 更多