awk:根据另一列的值有条件地更改字段的值
awk: change a field's value conditionally based on the value of another column
我有一个tablesnp150Common.txt
,其中第二个和第三个字段 and
可以相等也可以不相等。
如果相等,我想让</code>变成<code>-1
,这样:
chr1 10177 10177 rs367896724 - - -/C insertion near-gene-5
chr1 10352 10352 rs555500075 - - -/A insertion near-gene-5
chr1 11007 11008 rs575272151 C C C/G single near-gene-5
chr1 11011 11012 rs544419019 C C C/G single near-gene-5
chr1 13109 13110 rs540538026 G G A/G single intron
chr1 13115 13116 rs62635286 T T G/T single intron
chr1 13117 13118 rs62028691 A A C/T single intron
chr1 13272 13273 rs531730856 G G C/G single ncRNA
chr1 14463 14464 rs546169444 A A A/T single near-gene-3,ncRNA
变成:
chr1 10176 10177 rs367896724 - - -/C insertion near-gene-5
chr1 10351 10352 rs555500075 - - -/A insertion near-gene-5
chr1 11007 11008 rs575272151 C C C/G single near-gene-5
chr1 11011 11012 rs544419019 C C C/G single near-gene-5
chr1 13109 13110 rs540538026 G G A/G single intron
chr1 13115 13116 rs62635286 T T G/T single intron
chr1 13117 13118 rs62028691 A A C/T single intron
chr1 13272 13273 rs531730856 G G C/G single ncRNA
chr1 14463 14464 rs546169444 A A A/T single near-gene-3,ncRNA
我当前的命令改编自https://askubuntu.com/a/312843:
zcat < snp150/snp150Common.txt.gz | head | awk '{ if ( == ) =-1; print [=12=] }' | cut -f 2,3,4,5,8,9,10,12,16
给出相同的输出:
chr1 10177 10177 rs367896724 - - -/C insertion near-gene-5
chr1 10352 10352 rs555500075 - - -/A insertion near-gene-5
chr1 11007 11008 rs575272151 C C C/G single near-gene-5
chr1 11011 11012 rs544419019 C C C/G single near-gene-5
chr1 13109 13110 rs540538026 G G A/G single intron
chr1 13115 13116 rs62635286 T T G/T single intron
chr1 13117 13118 rs62028691 A A C/T single intron
chr1 13272 13273 rs531730856 G G C/G single ncRNA
chr1 14463 14464 rs546169444 A A A/T single near-gene-3,ncRNA
非常感谢任何帮助。
此答案纯粹基于对源文件格式的推测:
$ zcat snp150/snp150Common.txt.gz |
awk '
BEGIN { OFS="\t" } # field separators are most likely tabs
{
if ( == ) # based on cut these should be compared
=-1
print ,,,,,,,, # ... and there fields printed
}
NR==10 { exit }' # this replaces head
请记住:练习(除了吮吸以外的任何东西)都会减少吮吸。
我有一个tablesnp150Common.txt
,其中第二个和第三个字段 and
可以相等也可以不相等。
如果相等,我想让</code>变成<code>-1
,这样:
chr1 10177 10177 rs367896724 - - -/C insertion near-gene-5
chr1 10352 10352 rs555500075 - - -/A insertion near-gene-5
chr1 11007 11008 rs575272151 C C C/G single near-gene-5
chr1 11011 11012 rs544419019 C C C/G single near-gene-5
chr1 13109 13110 rs540538026 G G A/G single intron
chr1 13115 13116 rs62635286 T T G/T single intron
chr1 13117 13118 rs62028691 A A C/T single intron
chr1 13272 13273 rs531730856 G G C/G single ncRNA
chr1 14463 14464 rs546169444 A A A/T single near-gene-3,ncRNA
变成:
chr1 10176 10177 rs367896724 - - -/C insertion near-gene-5
chr1 10351 10352 rs555500075 - - -/A insertion near-gene-5
chr1 11007 11008 rs575272151 C C C/G single near-gene-5
chr1 11011 11012 rs544419019 C C C/G single near-gene-5
chr1 13109 13110 rs540538026 G G A/G single intron
chr1 13115 13116 rs62635286 T T G/T single intron
chr1 13117 13118 rs62028691 A A C/T single intron
chr1 13272 13273 rs531730856 G G C/G single ncRNA
chr1 14463 14464 rs546169444 A A A/T single near-gene-3,ncRNA
我当前的命令改编自https://askubuntu.com/a/312843:
zcat < snp150/snp150Common.txt.gz | head | awk '{ if ( == ) =-1; print [=12=] }' | cut -f 2,3,4,5,8,9,10,12,16
给出相同的输出:
chr1 10177 10177 rs367896724 - - -/C insertion near-gene-5
chr1 10352 10352 rs555500075 - - -/A insertion near-gene-5
chr1 11007 11008 rs575272151 C C C/G single near-gene-5
chr1 11011 11012 rs544419019 C C C/G single near-gene-5
chr1 13109 13110 rs540538026 G G A/G single intron
chr1 13115 13116 rs62635286 T T G/T single intron
chr1 13117 13118 rs62028691 A A C/T single intron
chr1 13272 13273 rs531730856 G G C/G single ncRNA
chr1 14463 14464 rs546169444 A A A/T single near-gene-3,ncRNA
非常感谢任何帮助。
此答案纯粹基于对源文件格式的推测:
$ zcat snp150/snp150Common.txt.gz |
awk '
BEGIN { OFS="\t" } # field separators are most likely tabs
{
if ( == ) # based on cut these should be compared
=-1
print ,,,,,,,, # ... and there fields printed
}
NR==10 { exit }' # this replaces head
请记住:练习(除了吮吸以外的任何东西)都会减少吮吸。