【问题标题】:Printing content in two file processing in awk在awk中处理两个文件中的打印内容
【发布时间】:2019-02-22 11:48:09
【问题描述】:

我指的是这个链接https://stackoverflow.com/a/54767231/11084572。 我有一个config file,其中2nd column is feature3rd column is action。我有另一个大文件,我需要将此文件的第一列与config file 的第一列匹配,并根据功能执行操作。

假设:在 File.txt 列中命名为Min (3rd col),Median (4th), Max(5th)

配置文件

Apple  All  Max
Car    abc  Median
Car    xyz  Min
Book   cvb  Median
Book   pqr  Max

文件.txt

Apple  first   10  20  30
Apple  second  20  30  40
Car    abc     10  20  30
Car    xyz     20  30  40
Car    wxyz    10  20  30
Book   cvb     60  70  80
Book   pqr     80  90  100

预期输出:

Apple  first   30
Apple  second  40
Car    abc     20
Car    xyz     20
Car    wxyz    10
Book   cvb     70
Book   pqr     100

上面的输出是在followinfg方法上生成的:

1)由于file.txt很大,所以如果config file的特征(第2列)为ALL,那么所有匹配的第1列都会根据config file的第3列执行动作。

2) 否则,如果config file 的第二列与**substring**file.txt 的第二列匹配,则执行

这是我尝试过的:

awk 'BEGIN {m["Min"]=3;m["Median"]=4;m["Max"]=5}

        NR==FNR{ arr[$1]=$2;brr[$1]=$3;next}
                ($1 in arr && arr[$1]=="All") {print $1,$2,$m[brr[$1]]}
                ($1 in arr && $2==arr[$1] ) {print $1 ,$2,$m[brr[$1]]}
' Config.txt File.txt

代码输出:

Apple  first   30
Apple  second  40
Book   pqr     100
Car    xyz     20 

上面的输出只打印匹配的第一列的一个字段(如Book cvb 70 不打印)。另外,我如何将字符串匹配为结束字符串(例如,config.txt 中定义的 xyz 与 file.txt 的 xyz and wxyz 匹配。

请帮助我解决上述挑战。谢谢!

【问题讨论】:

  • 在您的Assumption: 中,您的意思可能是File.txt 而不是Config.txt (?)
  • @downtheroad 是的,在 File.txt 中
  • @RATNESHTIWARI,在发布示例时请注意Car abc 200 行与您的第一个 Input_file 不匹配,如果是这种情况,请更新您的示例。
  • @RavinderSingh13,谢谢,我更新了。

标签: shell awk


【解决方案1】:

您预期的示例输出与您显示的 Input_file 示例不符(例如--> Car abc 200 其中file.txt 中没有200),如果我理解正确,请尝试以下操作。

awk '
BEGIN{
  b["min"]=3
  b["max"]=5
  b["median"]=4
}
FNR==NR{
  c[$1]
  ++d[$1]
  a[$1 d[$1]]=tolower($NF)
  next
}
($1 in c){
  if(e[$1]<d[$1]){
      ++e[$1]
  }
  else{
      e[$1]!=""?e[$1]:++e[$1]
  }
  print $1,$2,$b[a[$1 e[$1]]]
}' config.txt file.txt

输出如下。

Apple first 30
Apple second 40
Car abc 20
Car xyz 20
Car wxyz 10
Book cvb 70
Book pqr 100

说明:现在为上述代码添加说明。

awk '                                       ##Starting awk program here.
BEGIN{                                      ##Mentioning BEGIN section here which will be executed once and before reading Input_file only.
  b["min"]=3                                ##Creating an array named b whose index is string min and value is 3.
  b["max"]=5                                ##Creating an array named b whose index is string max and value is 5.
  b["median"]=4                             ##Creating an array named b whose index is string median and value is 4.
}                                           ##Closing BLOCK section here.
FNR==NR{                                    ##Checking condition FNR==NR which will be executed when 1st Input_file named config.txt is being read.
  c[$1]                                     ##Creating an array named c whose index is $1.
  ++d[$1]                                   ##Creating an array named d and with index is $1 whose value is keep increasing with 1 on its each occurence.
  a[$1 d[$1]]=tolower($NF)                  ##Creating an array named a whose index is $1 and value of d[$1] and value is small letters value of $NF(last column) of current line.
  next                                      ##Using next keyword of awk to skip all further statements from here.
}
($1 in c){                                  ##Checking conditions if $1 of current line is present of array c then do following.
  if(e[$1]<d[$1]){                          ##Checking condition if value of e[$1] is lesser than d[$1] then do following.
      ++e[$1]                               ##Creating array named e whose index is $1 and incrementing its value with 1 here.
  }
  else{                                     ##Using else for above if condition here.
      e[$1]!=""?e[$1]:++e[$1]               ##Checking if e[$1] is NULL then increment it with 1 or leave it as it is.
  }
  print $1,$2,$b[a[$1 e[$1]]]               ##Printing 1st, 2nd fields value along with field value of array b whose index is value of array a with index of $1 e[$1] here.
}' config.txt file.txt                      ##Mentioning Input_files here.

【讨论】:

  • 感谢您的快速回复!您能否简要解释一下代码。 tolower 在这里有什么需要。请解释你的方法!
  • @RATNESHTIWARI,是的,解释正在进行中。 BTW tolower 我正在使用将字符串制作成小写字母的形式。这样就不会混淆您的文件有 MAX OR Max` 或 max 等。因为它以小写字母更改它们,所以我不需要硬编码这些值来匹配。
  • @RATNESHTIWARI,现在添加了完整的解释,请检查一次。
  • 感谢以上解释。但解释需要使用上述变量/数组的目的及其对输出的影响。
  • @RATNESHTIWARI,目的是匹配 OP 的/你的预期输出,如果你更清楚地阅读解释,你可能会明白它的脉搏,让我知道你的困惑,然后可以尝试解决它们。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-20
  • 2021-06-15
  • 2016-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多