[英]If column B of file1 = column B of file2, replace column A file1 with column A of file2
I have two files of different lengths, with file2 being a big reference file, which I extract data from for file 1. 我有两个不同长度的文件,file2是一个大的参考文件,我从文件1中提取数据。
I have a line of awk which I normally tweak to do find and replace in my files, but it is always find and replace in the same column. 我有一行awk,我通常会在我的文件中调查并替换它,但它总是在同一列中找到并替换。
So for something like, if $1 of file1 = $7 of file2, replace $1 of file1 with $2 of file2, I would normally use: 所以对于类似的东西,如果file1的$ 1 = file2的$ 7,用file2的$ 2替换$ 1的file1,我通常会使用:
awk 'FNR==NR{a[$7]=$2;next}a[$1]{$1=a[$1]}1' file2 file1 > newfile
However, I am trying to think of a way to code: 但是,我试图想办法编码:
If $2 of file1 = $2 of file2, replace $1 file1 with $1 of file2. 如果file1的$ 2 = file2的$ 2,则将$ 1 file1替换为file2的$ 1。
But in the above code, I do not know which $1 refers to "find" and which $1 refers to "replace". 但在上面的代码中,我不知道哪个$ 1指的是“find”,哪个$ 1指的是“replace”。
file1 looks like file1看起来像
0 rs58108140 0 0 G A
0 rs189107123 0 0 C G
0 rs180734498 0 0 C T
file2 looks like file2看起来像
1 rs58108140 0 10583 G A 1:10583
1 rs189107123 0 10611 C G 1:10611
1 rs180734498 0 13302 C T 1:13302
Desired output would be: 期望的输出将是:
1 rs58108140 0 10583 G A
1 rs189107123 0 10611 C G
1 rs180734498 0 13302 C T
Thanks in advance for any help given. 提前感谢您提供的任何帮助。
这个单行将做:
awk 'NR==FNR{a[$2]=$1;b[$2]=$4;next}$2 in a{$1=a[$2];$4=b[$2]}7' f2 f1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.