将文件中的字符串查找到另一个文件

时间:2017-05-31 10:50:05

标签: bash shell awk

我想在FILE2中找到FILE1的每一列中的关键字,如果<BLANK>中的关键字不在FILE2中,则无论分隔符如何,都要打印FILE1

FILE1

XYZ=TRS-000|XYZ=TWR-000|UJU=909|GFT=879|JKP=908
XYZ=TRS-000|XYZ=TWR-000|GFT=879|JKP=908

FILE2

TRS-0
TWR
UJU
GFT-8

输出

XYZ=TRS-000|XYZ=TWR-000|UJU=909|GFT=879
XYZ=TRS-000|XYZ=TWR-000||GFT=879
到目前为止

SCRIPT

(此脚本在FILE2到FILE1列中找到完全匹配(使用=作为分隔符)。我无法弄清楚如何操作:如果来自FILE2 的字符串包含 FILE1。)

BEGIN{FS=OFS="|"}
NR==FNR{a[++i]=$1;next}
{
  d=""
  delete b
  for(j=1;j<=NF;j++){
    split($j,c,"-")
    b[c[1]]=$j;
  }
  for(j=1;j<=i;j++){
  d=d (d==""?"":OFS) (a[j] in b?b[a[j]]:"")
  }
  print d
}

`

2 个答案:

答案 0 :(得分:0)

根据您的示例输入,有两种模式可用于在拆分后确定关键字。此解决方案相应地设置索引i

BEGIN {FS=OFS="|"}

NR==FNR {a[$1]=NR; cols=NR; next}

{
    delete out
    for (f=1;f<=NF;++f) {
        i = ($f ~ /^.*=.*-.*$/) ? 2 : 1
        split($f, b, /[=-]/)
        if (b[i] in a) {
            out[a[b[i]]] = $f
        }
    }
    printf "%s", out[1]
    for (j=2; j<=cols; ++j) {
        printf "%s%s", OFS, out[j]
    }
    print ""
}


$ cat file1
XYZ=TRS-000|XYZ=TWR-000|UJU=909|GFT=879|JKP=908
XYZ=TRS-000|XYZ=TWR-000|GFT=879|JKP=908

$ cat file2
TRS
TWR
UJU
GFT

$ awk -f utl.awk file2 file1
XYZ=TRS-000|XYZ=TWR-000|UJU=909|GFT=879
XYZ=TRS-000|XYZ=TWR-000||GFT=879

答案 1 :(得分:0)

$ cat tst.awk
BEGIN { FS=OFS="|" }
NR==FNR { sub(/[^[:alpha:]].*/,""); keys[++numKeys]=$0; next }
{
    delete key2val
    for (i=1;i<=NF;i++) {
        for (keyNr=1; keyNr<=numKeys; keyNr++) {
            key = keys[keyNr]
            if ( $i ~ ("="key"-|^"key"=") ) {
                key2val[key] = $i
            }
        }
    }
    for (keyNr=1; keyNr<=numKeys; keyNr++) {
        key = keys[keyNr]
        printf "%s%s", key2val[key], (keyNr<numKeys?OFS:ORS)
    }
}

$ awk -f tst.awk file2 file1
XYZ=TRS-000|XYZ=TWR-000|UJU=909|GFT=879
XYZ=TRS-000|XYZ=TWR-000||GFT=879