Ber*_*rra 5 awk perl text-processing
我有 2 个文件作为输入:
文件 1(空格分隔)
ID POS a0 a1
SNP1 123 A C
SNP2 456 T C
SNP3 789 G A
Run Code Online (Sandbox Code Playgroud)
文件 2(空格分隔)
0 1 0 1 0 0 0 1
1 1 0 0 1 0 0 1
0 1 1 1 0 0 0 1
Run Code Online (Sandbox Code Playgroud)
所需的输出
A C A C A A A C
C C T T C T T C
G A A A G G G A
Run Code Online (Sandbox Code Playgroud)
文件 2 中的每一行代表文件 1 中的 1 行,诀窍是分别用 0 和 1 替换 a0 和 a1 中的相应字母。这只是一个小例子,真实的文件很大,超过60万行。
我正在寻找 awk 或 perl 解决方案。
作为一个难以辨认的 awk 语句
$ awk 'NR>1{a[0]=$3;a[1]=$4;getline<f;for(i=1;i<=NF;i++)$i=a[$i];print}' f=file2 file1
A C A C A A A C
C C T T C T T C
G A A A G G G A
Run Code Online (Sandbox Code Playgroud)
更具可读性:
awk '
# skip the header in file1
NR == 1 {next}
{
# read the values from the file1 line
a[0] = $3
a[1] = $4
# replace the current record with the corresponding line from the map file
getline < map_file
# and now substitute the 0/1 with the values
for (i=1; i<=NF; i++)
$i = a[$i]
print
}
' map_file=file2 file1
Run Code Online (Sandbox Code Playgroud)