awk 从一个文件中搜索列,如果匹配两个文件中的打印列
awk search column from one file, if match print columns from both files
我正在尝试比较文件 1 的第 1 列和文件 2 的第 3 列,如果它们匹配,则打印文件 1 的第一列和文件 2 的前两列。
这是每个文件的示例:
文件 1
Cre01.g000100
Cre01.g000500
Cre01.g000650
文件2
chromosome_1 71569 |655|Cre01.g000500|protein_coding|CODING|PAC:26902937|1|1)
chromosome_1 93952 |765|Cre01.g000650|protein_coding|CODING|PAC:26903448|11|1)
chromosome_1 99034 |1027|Cre01.g000100 |protein_coding|CODING|PAC:26903318|9|1)
期望的输出
Cre01.g000100 chromosome_1 99034
Cre01.g000500 chromosome_1 71569
Cre01.g000650 chromosome_1 93952
我一直在查看有些相似的各种线程,但我似乎无法让它打印两个文件中的列。以下是一些有些相关的链接:
awk compare 2 files, 2 fields different order in the file, print or merge match and non match lines
Obtain patterns from a file, compare to a column of another file, print matching lines, using awk
awk compare columns from two files, impute values of another column
Obtain patterns in one file from another using ack or awk or better way than grep?
我觉得我应该能够根据这些线程弄清楚,但是两天来我一直在尝试不同的代码变体,但我一无所获。
这是我尝试在我的文件中使用的一些代码:
awk 'FNR==NR{a[]=;next;}{print [=13=] ( in a ? a[]:"NA")}' file1 file2
awk 'NR==FNR{ a[]; next} ( in a) {print a[]}' file1 file2
awk 'FNR==NR{a[]=[=13=]; next}{print a[] [=13=]}' file1 file2
我知道我必须创建一个临时矩阵,其中包含 file1 的第一列(或 file2 的第 3 列),然后将其与另一个文件进行比较。如果匹配,则打印文件 1 的第一列和文件 2 的第 1 列和第 2 列。
感谢您的帮助!
三者中你的中间尝试最接近,但是:
- 您没有指定字段分隔符是
|
。
- 您没有分配给
a[]
。
您的示例输出与您想要的输出不一致(示例输出显示文件 1 的第 1 列和文件 2 的第 1 列;所需的输出据称是文件 1 的第 1 列和第 1 列以及2 来自文件 2,尽管这种解释取决于文件 2 中 </code> 的解释是两个管道符号之间的名称)。</p>
<p>在创建此答案时引用问题:</p>
<blockquote>
<p>… compare column 1 from file1 and column 3 from file 2, if they match then print the first column from file1 and the two first columns from file2.</p>
<pre><code>desired output
Cre01.g000100 chromosome_1 99034
Cre01.g000500 chromosome_1 71569
Cre01.g000650 chromosome_1 93952
我们可以观察到,如果文件 2 中的 </code> 等于文件 1 中的值,那么打印 <code>
就像保存的值一样容易。
所以,解决这个问题:
awk -F'|' 'NR==FNR { a[]=1; next } ( in a) { print , }' file1 file2
关键的变化是赋值给a[]
(和-F'|'
);其余的是装饰性的,可以根据您的要求进行调整(因为这个问题是自相矛盾的,所以很难给出更好的答案)。
你可以使用这个awk
:
awk -F '[| ]+' -v OFS='\t' 'NR==FNR{a[]= OFS ; next}
in a{print , a[]}' file2 file1
Cre01.g000100 chromosome_1 99034
Cre01.g000500 chromosome_1 71569
Cre01.g000650 chromosome_1 93952
我正在尝试比较文件 1 的第 1 列和文件 2 的第 3 列,如果它们匹配,则打印文件 1 的第一列和文件 2 的前两列。
这是每个文件的示例:
文件 1
Cre01.g000100
Cre01.g000500
Cre01.g000650
文件2
chromosome_1 71569 |655|Cre01.g000500|protein_coding|CODING|PAC:26902937|1|1)
chromosome_1 93952 |765|Cre01.g000650|protein_coding|CODING|PAC:26903448|11|1)
chromosome_1 99034 |1027|Cre01.g000100 |protein_coding|CODING|PAC:26903318|9|1)
期望的输出
Cre01.g000100 chromosome_1 99034
Cre01.g000500 chromosome_1 71569
Cre01.g000650 chromosome_1 93952
我一直在查看有些相似的各种线程,但我似乎无法让它打印两个文件中的列。以下是一些有些相关的链接:
awk compare 2 files, 2 fields different order in the file, print or merge match and non match lines
Obtain patterns from a file, compare to a column of another file, print matching lines, using awk
awk compare columns from two files, impute values of another column
Obtain patterns in one file from another using ack or awk or better way than grep?
我觉得我应该能够根据这些线程弄清楚,但是两天来我一直在尝试不同的代码变体,但我一无所获。 这是我尝试在我的文件中使用的一些代码:
awk 'FNR==NR{a[]=;next;}{print [=13=] ( in a ? a[]:"NA")}' file1 file2
awk 'NR==FNR{ a[]; next} ( in a) {print a[]}' file1 file2
awk 'FNR==NR{a[]=[=13=]; next}{print a[] [=13=]}' file1 file2
我知道我必须创建一个临时矩阵,其中包含 file1 的第一列(或 file2 的第 3 列),然后将其与另一个文件进行比较。如果匹配,则打印文件 1 的第一列和文件 2 的第 1 列和第 2 列。
感谢您的帮助!
三者中你的中间尝试最接近,但是:
- 您没有指定字段分隔符是
|
。 - 您没有分配给
a[]
。 您的示例输出与您想要的输出不一致(示例输出显示文件 1 的第 1 列和文件 2 的第 1 列;所需的输出据称是文件 1 的第 1 列和第 1 列以及2 来自文件 2,尽管这种解释取决于文件 2 中
</code> 的解释是两个管道符号之间的名称)。</p> <p>在创建此答案时引用问题:</p> <blockquote> <p>… compare column 1 from file1 and column 3 from file 2, if they match then print the first column from file1 and the two first columns from file2.</p> <pre><code>desired output Cre01.g000100 chromosome_1 99034 Cre01.g000500 chromosome_1 71569 Cre01.g000650 chromosome_1 93952
我们可以观察到,如果文件 2 中的
</code> 等于文件 1 中的值,那么打印 <code>
就像保存的值一样容易。
所以,解决这个问题:
awk -F'|' 'NR==FNR { a[]=1; next } ( in a) { print , }' file1 file2
关键的变化是赋值给a[]
(和-F'|'
);其余的是装饰性的,可以根据您的要求进行调整(因为这个问题是自相矛盾的,所以很难给出更好的答案)。
你可以使用这个awk
:
awk -F '[| ]+' -v OFS='\t' 'NR==FNR{a[]= OFS ; next}
in a{print , a[]}' file2 file1
Cre01.g000100 chromosome_1 99034
Cre01.g000500 chromosome_1 71569
Cre01.g000650 chromosome_1 93952