虽然读线,AWK $线 [英] While read line, awk $line
问题描述
我有一个包含数字的列表的文件。我有不同的条目和几个字段每个第二个文件。
I have a file that contains a list of numbers. I have a second file with various entries and several fields each.
我想要做的是让他们的第12场等于1号的所有行,并将其放置在一个新的文件,然后到第二个号码,依此类推。
What I want to do is to get all the lines whose 12th field is equal to the 1st number and place them in a new file, then to the second number, and so on.
我写了一个班轮是有道理的,但我不明白,为什么它不会工作。
I wrote a one-liner that makes sense, but I can't figure out why it won't work.
这是一个数字的列表:
猫 truncations_list.txt
3
318
407
412
7
与要排序的条目的文件是:
The file with the entries to be sorted is:
M00970:45:000000000-A42FD:1:1101:14736:1399 TGCCCAGTGCTCTGAATGTNNNNNTGAAGAAATTCAAGTAAGCGCGGGTCATCGGCNGGAGTAACTATGACTCTNTTAAGGAGGACCAATATGAACCANACNNNNNNNNNACTNTATCTAGGGTTCCCTGCACAGTATGTGNCC 79 TGCCCAGTGCTCTGAATGTNNNNNTGAAGAAATTCAAGTAAGCGCGGGTCATCGGCNGGAGTAACTATGACTCTNTTAA 65 GGAGGACCAATATGAACCANACNNNNNNNNNACTNTATCTAGGGTTCCCTGCACAGTATGTGNCC 79S65M 1 81 TGCCCAGTGCTCTGAATGTNNNNNTGAAGAAATTCAAGTAAGCGCGGGTCATCGGCNGGAGTAACTATGACTCTNTTAAGG -2 318
M00970:45:000000000-A42FD:1:1101:15371:1399 TGCCCAGTGCTCTGAATGTNNNNNTGAAGAAATTCAAGTAAGCGCGGGTCAACGGCNGGAGTAACTATGACTCTNTTAAGGAGTCGGTGTTCACATGCNATNNNNNNNNNCAGNCGAACTTGATGAAGAACGTCGACGTGTNGG 83 TGCCCAGTGCTCTGAATGTNNNNNTGAAGAAATTCAAGTAAGCGCGGGTCAACGGCNGGAGTAACTATGACTCTNTTAAGGAG 61 TCGGTGTTCACATGCNATNNNNNNNNNCAGNCGAACTTGATGAAGAACGTCGACGTGTNGG 83S61M 1 81 TGCCCAGTGCTCTGAATGTNNNNNTGAAGAAATTCAAGTAAGCGCGGGTCAACGGCNGGAGTAACTATGACTCTNTTAAGG 2 407
这是我的命令:
file="truncations_list.txt"
while read line; do awk '$12==$line' R2_Output.txt >reads_$line.txt ; done <"$file"
此命令将创建的所有文件reads_412.txt等,但所有的文件都是空的。
This command will create all the files "reads_412.txt", etc, but all the files are empty.
我AP preciate您的帮助!
I appreciate your help!
推荐答案
您主要问题是,awk程序是用单引号,因此,$线变量是永远不会扩大。快速修复
Your main problem is that the awk program is in single quotes, so the "$line" variable is never expanded. The quick fix is
awk -v num=$line '$12==num' R2_Output.txt
不过,不这样做。您正在阅读的输出文件,一旦在数字文件中的每一行。你可以把它只需通过每个文件只有一个时间阅读工作:
But, don't do that. You're reading the output file once for each line in the numbers file. You can make it work by just reading through each file only one time:
awk '
# read the list of numbers in truncations_list
FNR == NR {
num[$1]
next
}
# process each line of the output file
# any lines with an "unknown" $12 will be ignored
$12 in num {
f = "reads_" $12 ".txt"
print >> f
}
' truncations_list.txt R2_Output.txt
这篇关于虽然读线,AWK $线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!