2

我有三个文件 G_P_map.txt、G_S_map.txt 和 S_P_map.txt。我必须使用 awk 组合这三个文件。示例内容如下——

(G_P_map.txt 包含)

test21g|A-CZ|1mos
test21g|A-CZ|2mos
 ...

(G_S_map.txt 包含)

nwtestn5|A-CZ
nwtestn6|A-CZ
 ...

(S_P_map.txt 包含)

3mos|nwtestn5
4mos|nwtestn6

预期输出:

1mos, 3mos
2mos, 4mos

这是我尝试过的代码。我能够将前两个结合起来,但我不能与第三个结合起来。

awk -F"|" 'NR==FNR {file1[$1]=$1; next} {$2=file[$1]; print}' G_S_map.txt S_P_map.txt 

非常感谢任何想法/帮助。提前致谢!

4

2 回答 2

3

我会看看joincut的组合。

于 2012-07-10T14:43:16.247 回答
2

GNU AWK ( gawk) 4 有BEGINFILE并且ENDFILE非常适合这个。但是,该gawk手册包含一个功能,可为大多数版本的 AWK 提供此功能。

#!/usr/bin/awk

BEGIN {
    FS = "|"
}

function beginfile(ignoreme) {
    files++
}

function endfile(ignoreme) {
    # endfile() would be defined here if we were using it
}

FILENAME != _oldfilename \
{
    if (_oldfilename != "")
        endfile(_oldfilename)
    _oldfilename = FILENAME
    beginfile(FILENAME)
}

END   { endfile(FILENAME) }

files == 1 {    # save all the key, value pairs from file 1
    file1[$2] = $3
    next
}

files == 2 {    # save all the key, value pairs from file 2
    file2[$1] = $2
    next
}

files == 3 {    # perform the lookup and output
    print file1[file2[$2]], $1
}    

# Place the regular END block here, if needed. It would be in addition to the one above (there can be more than one)

像这样调用脚本:

./scriptname G_P_map.txt G_S_map.txt S_P_map.txt
于 2012-07-10T15:15:53.827 回答