简体   繁体   English

在if语句中使用awk

[英]Using awk in if statements

I have a data file that looks like this: 我有一个数据文件,看起来像这样:

1   .   0   10109   AA  AA
1   .   0   10123   C   CCCT
1   .   0   10133   A   AAC
1   .   0   10134   A   ACAAC
1   .   0   10140   A   ACCCTAAC
1   .   0   10143   C   CTACT
1   rs144773400 0   10144   T   TA
1   .   0   10146   AC  A
1   .   0   10147   G   C

In the instance of "." 以“。”为例。 in the second column, I would like to replace it with a merged output of columns 1 and 4, like this: 在第二列中,我想用列1和4的合并输出替换它,如下所示:

1   1:10109 0   10109   AA  AA
1   1:10123 0   10123   C   CCCT
1   1:10133 0   10133   A   AAC
1   1:10134 0   10134   A   ACAAC
1   1:10140 0   10140   A   ACCCTAAC
1   1:10143 0   10143   C   CTACT
1   rs144773400 0   10144   T   TA
1   1:10146 0   10146   AC  A
1   1:10147 0   10147   G   C

I've been attempting to do this with an if/then statement... but I know I have the syntax wrong, I'm just not sure how wrong. 我一直在尝试使用if / then语句执行此操作...但是我知道我的语法错误,只是不确定如何错误。

if [$2 -eq "." /data/pathtofile]
then 
    awk '{print $1 ":" $4}'
else 
    awk '{print $2}' >> "/data/cleanfile"
fi 

What am I missing? 我想念什么?

You could do this through awk itself. 您可以通过awk本身执行此操作。

awk -v FS="\t" -v OFS="\t" '$2=="."{$2=$1":"$4}{$1=$1}1' file

OR 要么

$ awk '$2=="."{$2=$1":"$4}{$1=$1}1' file
1 1:10109 0 10109 AA AA
1 1:10123 0 10123 C CCCT
1 1:10133 0 10133 A AAC
1 1:10134 0 10134 A ACAAC
1 1:10140 0 10140 A ACCCTAAC
1 1:10143 0 10143 C CTACT
1 rs144773400 0 10144 T TA
1 1:10146 0 10146 AC A
1 1:10147 0 10147 G C

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM