如何用另一列中的字符串替换一列中的值?

如何用另一列中的字符串替换一列中的值?

我有一个这样的文件:

head new_fileset_prefix_17.sampleT
ID_1 ID_2 missing
0 0 0
0 fam1000_G1000 0
0 fam1001_G1001 0
0 fam1003_G1003 0
0 fam1005_G1005 0
0 fam1009_G1009 0
...

我想得到这个输出:

ID_1 ID_2 missing
0 0 0
fam1000 G1000 0
fam1001 G1001 0
fam1003 G1003 0
fam1005 G1005 0
fam1009 G1009 0
...

答案1

对单个字符串执行简单 s/old/new 的最佳工具是 sed:

$ sed 's/^[0-9]* \([^_]*\)_/\1 /' file
ID_1 ID_2 missing
0 0 0
fam1000 G1000 0
fam1001 G1001 0
fam1003 G1003 0
fam1005 G1005 0
fam1009 G1009 0

但在 awk 中你可以这样做:

$ awk '/^[0-9]/ && sub(/_/," ",$2){$0=$2 OFS $3} 1' file
ID_1 ID_2 missing
0 0 0
fam1000 G1000 0
fam1001 G1001 0
fam1003 G1003 0
fam1005 G1005 0
fam1009 G1009 0

答案2

awk 'NR<3 { print; next; }; { split($2,a,"_"); print a[1] " " a[2] " " $3; }' file

相关内容