我有三个文件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 任何想法/帮助都是非常感谢的。提前感谢!
发布于 2012-07-10 23:15:54
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-10 22:43:16
我会看一下join和cut的组合。
https://stackoverflow.com/questions/11415731
复制相似问题