【问题标题】:Awk command to compare specific columns in file1 to file2 and display output用于将 file1 中的特定列与 file2 进行比较并显示输出的 awk 命令
【发布时间】:2017-11-09 13:48:52
【问题描述】:

文件1

111,222,560,0.7
111,333,560,0.2
111,444,560,0.1

文件2

2017,111,560,0.0537
2018,111,560,0.0296
2019,111,560,0.0624

期望的输出:

2017,111,560,0.0537,222,0.7
2018,111,560,0.0296,222,0.7
2019,111,560,0.0624,222,0.7
2017,111,560,0.0537,333,0.2
2018,111,560,0.0296,333,0.2
2019,111,560,0.0624,333,0.2
2017,111,560,0.0537,444,0.1
2018,111,560,0.0296,444,0.1
2019,111,560,0.0296,444,0.1

我尝试了 awk NR==FNR 命令,但它只显示最后匹配的...

读取每一行并检查 file1 的第 1 列和第 3 列是否存在于 file2 中:

2017,111,560,0.0537,444,0.1
2018,111,560,0.0296,444,0.1
2019,111,560,0.0296,444,0.1

【问题讨论】:

  • awk -F, 'BEGIN{OFS=","} NR==FNR{a[$1$3]=$2","$4;next}($2$3 in a){print $0, a[$2$3]}' File1 File2 这是我使用的命令,但我想查看所有匹配项.. 请帮助

标签: awk


【解决方案1】:

我尝试了 awk NR==FNR 命令,但它只显示最后一个 匹配...

读取每一行并检查file1的第1列和第3列是否存在于file2中:

使用awksort

awk 'BEGIN{
           # set input and output field separator
           FS=OFS=","               
     }
     # read first file f1
     # index key field1 and field3 of file1 (f1)
     {
         k=$1 FS $3
     }

     # save 2nd and last field of file1 (f1) in array a, key being k
     FNR==NR{
         a[k]=(k in a ? a[k] RS:"") $2 OFS $NF; 

         # stop processing go to next line
         next
     }

     # read 2nd file f2 from here 
     # 2nd and 3rd field of fiel2 (f2) used as key
     {
         k=$2 FS $3
     }

     # if key exists in array a
     k in a{
         # split array value by RS row separator, and put it in array t
         split(a[k],t,RS); 

         # iterate array t, print and sort
         for(i=1; i in t; i++)
              print $0,t[i] | "sort -t, -nk5" 
     }
     ' f1 f2

测试结果:

$ cat f1
111,222,560,0.7
111,333,560,0.2
111,444,560,0.1

$ cat f2
2017,111,560,0.0537
2018,111,560,0.0296
2019,111,560,0.0624

$ awk 'BEGIN{FS=OFS=","}{k=$1 FS $3}FNR==NR{a[k]=(k in a ? a[k] RS:"") $2 OFS $NF; next}{k=$2 FS $3}k in a{split(a[k],t,RS); for(i=1; i in t; i++)print $0,t[i] | "sort -t, -nk5" }' f1 f2
2017,111,560,0.0537,222,0.7
2018,111,560,0.0296,222,0.7
2019,111,560,0.0624,222,0.7
2017,111,560,0.0537,333,0.2
2018,111,560,0.0296,333,0.2
2019,111,560,0.0624,333,0.2
2017,111,560,0.0537,444,0.1
2018,111,560,0.0296,444,0.1
2019,111,560,0.0624,444,0.1

【讨论】:

  • 哦,是的,你救了我 :) 你真是个天才!
  • 根据您的要求,它会检查第 1 列和第 3 列是否存在于其他文件中
【解决方案2】:

关注awk 可能对您有所帮助。

awk -F, '
FNR==NR{
  a[FNR]=$0;
  next
}
{
  for(i=1;i<=length(a);i++){
    print a[i] FS $2 FS $NF
}
}'   Input_file2  Input_file1

也为代码添加解释如下。

awk -F, '                   ##Setting field separator as comma here for all the lines.
FNR==NR{                    ##Using FNR==NR condition which will be only TRUE then first Input_file named File2 is being read.
                            ##FNR and NR both indicates the number of lines for a Input_file only difference is FNR value will be RESET whenever a new file is being read and NR value will be keep increasing till all Input_files are read.
  a[FNR]=$0;                ##Creating an array named a whose index is FNR(current line) value and its value is current line value.
  next                      ##Using next statement will sip all further statements now.
}
{
  for(i=1;i<=length(a);i++){##Starting a for loop from variable i value from 1 to length of array a value. This will be executed on 2nd Input_file reading.
    print a[i] FS $2 FS $NF ##Printing the value of array a whose index is variable i and printing 2nd and last field of current line.
}
}' File2 File1              ##Mentioning the Input_file names here.

【讨论】:

  • 嗨 ravinder,谢谢。我在尝试执行您的解决方案时遇到错误:awk:无法读取 a 的值。它是一个数组名称。输入行号为 1。文件为 cml_rdf_delieo_bom_143.dat_spread。源代码行号为 7。
【解决方案3】:

另一个join/awk

$ join -t, -j99 file2 file1 | 
  awk -F, -v OFS=, '$3==$6 && $4==$8 {print $2,$3,$4,$5,$7,$9}'

【讨论】:

  • 嗨,你能解释一下吗:) -j99 是什么意思?
猜你喜欢
  • 2020-03-24
  • 2016-01-02
  • 1970-01-01
  • 2021-08-17
  • 1970-01-01
  • 1970-01-01
  • 2023-04-08
  • 2010-12-21
  • 2014-05-26
相关资源
最近更新 更多