簡體   English   中英

您如何從文件中的列中 grep/awk?

[英]How do you grep/awk from a column in a file?

我有一個名為 IDs_list.txt 的 ID 文件,我想使用它來從具有數百個 ID 的第二個文件中提取信息,其中許多 ID 不在我的特定 IDS_list.txt 中。

我已經嘗試過 if 和 grep 的組合,但我的結果一直是空的。

這是我正在嘗試做的事情和我所做的事情的一個例子。

cat IDS_list.txt | head -n 4
24
43
56
69

cat sample1.txt | head -n 4
NODE_1_length_148512_cov_24.5066,gi|573017271|gb|CP006568.1|,148512,4513140,8,7289,86.545,0.0,13461,24,madeup species 1
NODE_2_length_122550_cov_25.719,gi|84778498|dbj|AP008232.1|,122550,4171146,13,12690,93.693,0.0,23435,244,madeup species 2
NODE_3_length_103385_cov_25.9802,gi|84778498|dbj|AP008232.1|,103385,4171146,6,4243,88.782,0.0,7836,43,madeup species 3
NODE_4_length_101672_cov_25.6536,gi|84778498|dbj|AP008232.1|,101672,4171146,7,4139,86.799,0.0,7644,955,long name here

ID 在第 10 列。

我需要提取IDS_list.txt 中ID 所在的所有行。

所以我的 output 應該是:

NODE_1_length_148512_cov_24.5066,gi|573017271|gb|CP006568.1|,148512,4513140,8,7289,86.545,0.0,13461,24,madeup species 1
NODE_3_length_103385_cov_25.9802,gi|84778498|dbj|AP008232.1|,103385,4171146,6,4243,88.782,0.0,7836,43,madeup species 3

我試過了:

for file in sample?.txt; do awk 'FNR==NR{arr[$0];next} ($10 in arr)' IDs_list.txt $file; done

什么都沒有出來。 這個例子我取自另一個堆棧溢出問題。

for i in $(cat IDs_list.txt); do awk -F"," '$10 == $i' sample1.txt; done

但這會打印一個 output 很多次,因為我逐行遍歷 IDs_list.txt,所以這不是我想要的。 我將獲得第一條 output 行可能數百次,因為我的 IDs_list.txt 有數百個 ID。

然后我嘗試了 grep 和 awk 但這也沒有用。 我的語法關閉了。

for file in sample?.txt; do for i in $(cat IDs_list.txt); do grep -w '$i' $file; done; done

這里沒有 output。 我的邏輯是,對於每個示例文件,我想 grep 包含在 IDs_list.txt 中找到的 ID 的行。 但是我不喜歡不調用特定的第 10 列,因為 ID 有時會出現在其他實際上不是 ID 的列中。

任何 eloquent 方法在使用 grep 或 awk 或兩者兼而有之的 for 循環中執行此操作?

您可以使用此awk

awk -F, 'NR==FNR {ids[$1]; next} $10 in ids' IDs_list.txt sample.txt

NODE_1_length_148512_cov_24.5066,gi|573017271|gb|CP006568.1|,148512,4513140,8,7289,86.545,0.0,13461,24,madeup species 1
NODE_3_length_103385_cov_25.9802,gi|84778498|dbj|AP008232.1|,103385,4171146,6,4243,88.782,0.0,7836,43,madeup species 3

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM