为awk列说明符传递bash变量
关于将shell变量传递给awk有很多线程,我已经很容易地解决了这一问题,但是我想要传递的变量是列说明符变量(为awk列说明符传递bash变量,bash,shell,variables,awk,Bash,Shell,Variables,Awk,关于将shell变量传递给awk有很多线程,我已经很容易地解决了这一问题,但是我想要传递的变量是列说明符变量($1,$2等) 鉴于shell也将这些变量用作默认命令行参数变量,这会让人感到困惑 在这个脚本中,我只是将两个文件排序并合并在一起,但是为了开始稍微概括脚本,我希望能够在命令行上指定关键文件中的字段,awk应该将该字段作为其排序说明符 我做错了什么?(我只是刚刚开始接触awk,oneliner是根据awk稍作修改的 要排序的文件: 因此,我需要根据keyfile中的第3列和要排序的文件中
$1,$2
等)
鉴于shell也将这些变量用作默认命令行参数变量,这会让人感到困惑
在这个脚本中,我只是将两个文件排序并合并在一起,但是为了开始稍微概括脚本,我希望能够在命令行上指定关键文件中的字段,awk应该将该字段作为其排序说明符
我做错了什么?(我只是刚刚开始接触awk,oneliner是根据awk稍作修改的
要排序的文件:
因此,我需要根据keyfile中的第3列和要排序的文件中的第1列对行进行排序和匹配
结果文件:(第3列和第4列的重复内容是我计划整理的内容)
当您传递
awk-va=“$field”
时,awk变量a
的规范仅适用于单个awk
命令。您不能期望a
在完全不同的awk
调用中可用
因此,您需要直接将其放置到位:
或者在您的情况下:
field=1
awk -v a="$field" '
NR==FNR {
o[FNR]=$a;
next;
}
{ t[$1] = $0 }
END {
for(x=1; x<=FNR; x++) {
y=o[x]
printf("%s\t%s\n", y, t[y])
}
}' "$keyfile" "$filetosort"
字段=1
awk-v a=“$field””
NR==FNR{
o[FNR]=一美元;
下一个
}
{t[$1]=$0}
结束{
对于(x=1;x我将添加此作为答案,因为它确实解决了我提出的问题,尽管Charles对我所犯错误的(无数)方面给出了极好的建议
用Charles关于单独的awk命令的观点修改上述代码,我现在可以调用以下内容(抱歉,是的,它仍在使用paste
)
错误。您正在将keyfile
作为可执行文件运行,并将其作为可执行文件运行的输出粘贴到第一列--这真的是您想要的吗?以及您的awk-v a=“$field”
实际上根本没有一个awk程序的文本传递给它。顺便说一句,你完全可以使用join
来达到这个目的——根本不需要awk
来满足用例。我强烈建议,强烈推荐使用join
来完成这个任务。awk
解决方案不能处理大于我的输入t可以一次放入内存——但因为join
需要排序输入,所以它可以依赖于流式输入,而sort
的GNU实现可以将不适合内存的文件转储到磁盘。您的问题是要包括简明的、可测试的样本输入和预期的输出,因为您的文本要求不明确,我们需要当一个文件中的行或字段比另一个文件中的行或字段多/少时(以您试图用作键值的为准),请确保包含(或至少描述)所需的雨天行为。啊,我明白了,这是有道理的。我正在读的一个线程(我现在找不到)将它们显示为单独的shell命令,我想这让我很困惑。这个awk解决方案几乎是完美的,除了我需要维护keyfile中的整行,实际上,它只是维护包含排序键的行。我用输入文件编辑了这个问题,希望它更清晰。Re:“not sorted”排序规则需要匹配。据我所知,它们应该是相同的。什么是连接查找?我必须为它指定一个标志,只考虑一个特定的列或什么?是的,您需要指定特定的列,并且该列需要是文档排序的一个。谢谢你的帮助。
PVClumt18 PAK_2199 PAK_01997
PVClopt2 PAK_2091 PAK_01895
PVCcif7 PAK_1975 PAK_01793
PVClopT12 PAU_02101 PAU_02063
PVCpnf20 PAK_3524 PAK_03184
PVClopt3 PAK_2090 PAK_01894
PVClopT11 PAU_02102 PAU_02064
PVCunit2_11 plu1698 PLT_01726
PVClumT9 afp10 PAU_02198
PVCunit2_17 plu1692 PLT_01720
PAU_02064 1pqx 1pqx_A 37.4 13 0.00035 31.4 >1pqx_A Conserved hypothetical protein; ZR18,structure, autostructure,spins,autoassign, northeast structural genomics consortium; NMR {Staphylococcus aureus subsp} SCOP: d.267.1.1 PDB: 2ffm_A 2m6q_A 2m8w_A
PAK_01997 5ftj 5ftj_A 99.9 1.6e-26 4.2e-31 229.2 >5ftj_A Transitional endoplasmic reticulum ATPase; hydrolase, single-particle, AAA ATPase; HET: ADP OJA; 2.30A {Homo sapiens} PDB: 3cf1_A* 3cf3_A* 3cf2_A* 5ftk_A* 5ftl_A* 5ftm_A* 5ftn_A* 1r7r_A* 5c19_A 5c1b_A* 5c18_A* 3cf0_A*
PAK_01894 3j9q 3j9q_A 99.9 1.8e-29 4.6e-34 215.9 >3j9q_A Sheath; pyocin, bacteriocin, sheath, structural protein; 3.50A {Pseudomonas aeruginosa}
PAK_03184 1xju 1xju_A 99.4 4.1e-17 1.1e-21 98.8 >1xju_A Lysozyme; secreted inactive conformation, hydrolase; 1.07A {Enterobacteria phage P1} SCOP: d.2.1.3
PAK_01793 5a3a 5a3a_A 50.8 6 0.00016 31.4 >5a3a_A SIR2 family protein; transferase, P-ribosyltransferase, metalloprotein, NAD-depen lipoylation, regulatory enzyme, rossmann fold; 1.54A {Streptococcus pyogenes} PDB: 5a3b_A* 5a3c_A*
PLT_01720 3ggm 3ggm_A 54.2 4.9 0.00013 26.2 >3ggm_A Uncharacterized protein BT9727_2919; bacillus cereus group., structural genomics, PSI-2, protein structure initiative; 2.00A {Bacillus thuringiensis serovarkonkukian}
PLT_01726 3h2t 3h2t_A 96.8 8e-06 2.1e-10 82.6 >3h2t_A Baseplate structural protein GP6; viral protein, virion; 3.20A {Enterobacteria phage T4} PDB: 3h3w_A 3h3y_A
PAK_01895 3j9q 3j9q_A 100.0 2.5e-35 6.4e-40 248.6 >3j9q_A Sheath; pyocin, bacteriocin, sheath, structural protein; 3.50A {Pseudomonas aeruginosa}
PAU_02198 4jiv 4jiv_D 69.6 1.6 4.2e-05 27.5 >4jiv_D VCA0105, putative uncharacterized protein; PAAR-repeat motif, membrane piercing, type VI secretion SYST vibrio cholerae VGRG2; HET: PLM STE ELA; 1.90A {Vibrio cholerae o1 biovar eltor}
PAU_02063 4yap 4yap_A 31.1 20 0.00052 29.1 >4yap_A Glutathione S-transferase homolog; GSH-lyase GSH-dependent; 1.11A {Sphingobium SP} PDB: 4g10_A 4yav_A*
PVClumt18 PAK_2199 PAK_01997 PAK_01997 5ftj 5ftj_A 99.9 1.6e-26 4.2e-31 229.2 >5ftj_A Transitional endoplasmic reticulum ATPase; hydrolase, single-particle, AAA ATPase; HET: ADP OJA; 2.30A {Homo sapiens} PDB: 3cf1_A* 3cf3_A* 3cf2_A* 5ftk_A* 5ftl_A* 5ftm_A* 5ftn_A* 1r7r_A* 5c19_A 5c1b_A* 5c18_A* 3cf0_A*
PVClopt2 PAK_2091 PAK_01895 PAK_01895 3j9q 3j9q_A 100.0 2.5e-35 6.4e-40 248.6 >3j9q_A Sheath; pyocin, bacteriocin, sheath, structural protein; 3.50A {Pseudomonas aeruginosa}
PVCcif7 PAK_1975 PAK_01793 PAK_01793 5a3a 5a3a_A 50.8 6 0.00016 31.4 >5a3a_A SIR2 family protein; transferase, P-ribosyltransferase, metalloprotein, NAD-depen lipoylation, regulatory enzyme, rossmann fold; 1.54A {Streptococcus pyogenes} PDB: 5a3b_A* 5a3c_A*
PVClopT12 PAU_02101 PAU_02063 PAU_02063 4yap 4yap_A 31.1 20 0.00052 29.1 >4yap_A Glutathione S-transferase homolog; GSH-lyase GSH-dependent; 1.11A {Sphingobium SP} PDB: 4g10_A 4yav_A*
PVCpnf20 PAK_3524 PAK_03184 PAK_03184 1xju 1xju_A 99.4 4.1e-17 1.1e-21 98.8 >1xju_A Lysozyme; secreted inactive conformation, hydrolase; 1.07A {Enterobacteria phage P1} SCOP: d.2.1.3
PVClopt3 PAK_2090 PAK_01894 PAK_01894 3j9q 3j9q_A 99.9 1.8e-29 4.6e-34 215.9 >3j9q_A Sheath; pyocin, bacteriocin, sheath, structural protein; 3.50A {Pseudomonas aeruginosa}
PVClopT11 PAU_02102 PAU_02064 PAU_02064 1pqx 1pqx_A 37.4 13 0.00035 31.4 >1pqx_A Conserved hypothetical protein; ZR18,structure, autostructure,spins,autoassign, northeast structural genomics consortium; NMR {Staphylococcus aureus subsp} SCOP: d.267.1.1 PDB: 2ffm_A 2m6q_A 2m8w_A
PVCunit2_11 plu1698 PLT_01726 PLT_01726 3h2t 3h2t_A 96.8 8e-06 2.1e-10 82.6 >3h2t_A Baseplate structural protein GP6; viral protein, virion; 3.20A {Enterobacteria phage T4} PDB: 3h3w_A 3h3y_A
PVClumT9 afp10 PAU_02198 PAU_02198 4jiv 4jiv_D 69.6 1.6 4.2e-05 27.5 >4jiv_D VCA0105, putative uncharacterized protein; PAAR-repeat motif, membrane piercing, type VI secretion SYST vibrio cholerae VGRG2; HET: PLM STE ELA; 1.90A {Vibrio cholerae o1 biovar eltor}
PVCunit2_17 plu1692 PLT_01720 PLT_01720 3ggm 3ggm_A 54.2 4.9 0.00013 26.2 >3ggm_A Uncharacterized protein BT9727_2919; bacillus cereus group., structural genomics, PSI-2, protein structure initiative; 2.00A {Bacillus thuringiensis serovarkonkukian}
$ bashvar="2"
$ echo 'foo bar baz' | awk -v awkvar="$bashvar" '{print $awkvar}'
bar
field=1
awk -v a="$field" '
NR==FNR {
o[FNR]=$a;
next;
}
{ t[$1] = $0 }
END {
for(x=1; x<=FNR; x++) {
y=o[x]
printf("%s\t%s\n", y, t[y])
}
}' "$keyfile" "$filetosort"
#!/bin/bash
keyfile="$1"
filetosort="$2"
indexfield="$3"
paste "$keyfile" <(awk -v field="$indexfield" 'NR==FNR{o[FNR]=$field; next} {t[$1]=$0} END{for(x=1; x<=FNR; x++){y=o[x]; print t[y]}}' "$keyfile" "$filetosort")
PVCunit2_5 plu1704 PLT_01732 PLT_01732 4etv 4etv_A 39.0 12 0.00032 27.6 >4etv_A Ryanodine receptor 2; phosphorylation, cardiac, metal transport; 1.65A {Mus musculus}
PVCunit2_4 plu1705 PLT_01733 PLT_01733 3j9q 3j9q_A 99.9 7.2e-30 1.9e-34 219.0 >3j9q_A Sheath; pyocin, bacteriocin, sheath, structural protein; 3.50A {Pseudomonas aeruginosa}
XVC_pnf15 XBW1_RS06910 XBW1_RS06910 XBW1_RS06910 1fi0 1fi0_A 69.2 1.7 4.4e-05 22.8 >1fi0_A VPR protein, R ORF protein; helix, viral protein; NMR {Synthetic} SCOP: j.11.1.1
PVCcif7 PAU_01999 PAU_01967 PAU_01967 5a3a 5a3a_A 47.5 7.3 0.00019 30.9 >5a3a_A SIR2 family protein; transferase, P-ribosyltransferase, metalloprotein, NAD-depen lipoylation, regulatory enzyme, rossmann fold; 1.54A {Streptococcus pyogenes} PDB: 5a3b_A* 5a3c_A*
PVClumT15 PAU_02233 PAU_02192 PAU_02192 1tdp 1tdp_A 22.1 37.0 0.00096 27.2 >1tdp_A Carnobacteriocin B2 immunity protein; four-helix bundle, antimicrobial protein; NMR {Carnobacterium maltaromaticum} SCOP: a.29.8.1
XVC_pnf3 XBW1_RS06850 XBW1_RS06850 XBW1_RS06850 3eaa 3eaa_A 87.7 0.13 3.4e-06 35.7 >3eaa_A EVPC; T6SS, unknown function; 2.79A {Edwardsiella tarda}
PVCunit1_4 afp4 PAU_02778 PAU_02778 3j9q 3j9q_A 99.9 3.6e-29 9.5e-34 214.6 >3j9q_A Sheath; pyocin, bacteriocin, sheath, structural protein; 3.50A {Pseudomonas aeruginosa}
PVCunit2_3 plu1706 PLT_01734 PLT_01734 3j9q 3j9q_A 100.0 1.6e-34 4.3e-39 253.7 >3j9q_A Sheath; pyocin, bacteriocin, sheath, structural protein; 3.50A {Pseudomonas aeruginosa}
PVClumt17 PAK_2200 PAK_01998 PAK_01998 3k8p 3k8p_C 34.7 16.0 0.00041 34.1 >3k8p_C DSL1, KLLA0C02695P; intracellular trafficking, DSL1 complex, multisubunit tethering complex, snare proteins; 2.60A {Kluyveromyces lactis}
PVClopT12 PAU_02101 PAU_02063 PAU_02063 4yap 4yap_A 31.1 20 0.00052 29.1 >4yap_A Glutathione S-transferase homolog; GSH-lyase GSH-dependent; 1.11A {Sphingobium SP} PDB: 4g10_A 4yav_A*