简体   繁体   English

如果file1的B列= file2的B列,则将A列file1替换为file2的A列

[英]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.

相关问题 我想从 file1 中的第 1 列和第 2 列中找到一些与 file2 中的第 1 列匹配的字符串/单词,并替换为 file2 中的第 2 列字符串/单词 - I want to find some strings/words from column 1 and 2 in file1 that match column 1 in file2 and replace with column 2 strings/words in file2 如何根据与file2的列匹配删除file1中的行 - How to delete lines in file1 based on column match with file2 将带有一列的 file1 与来自 file2 的两列进行比较 - Compare file1 with one column to two columns from file2 如何基于文件/ file1(仅)第一列与linux中的file2的匹配信息从file1提取行? - how to extract rows from file1 based on matching information of its/file1 (only)first column with file2 in linux? 如何将file1的每一列附加到file2的特定字段并创建一个新的输出文件? - How to append each column of file1 to a specific field of file2 and make a new output file? 当 ID 与 file2 匹配时,从 file1 复制一列,并根据文件 2 打印 output - copy a column from file1 when the ID's matches to file2 and print output according to file 2 如何使用awk删除Ubuntu的file2中存在列1值的file1行? - How to use awk to delete lines of file1 whose column 1 values exist in file2 in Ubuntu? 可能是grep,但仍然无法获得如何读取file1中的行并将其粘贴为file2中的列的方法 - Probably grep, but still do not get how to read row in file1 and paste it as a column in file2 获取不在file2中的file1行 - Get lines of file1 which are not in file2 从File2中提取行已找到File1 - Extract lines from File2 already found File1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM