我有一个如下列表:
group Values
0 aa_bb_25_0_1
0 273
0 60
1 aa_bb_7_0_2
1 335
2 aa_bb_65_1_1
2 456
3 aa_bb_98_0_1
我想采取以" aa_bb"开头的唯一第一个条目。在相应的组内并修改tabe如下
group Values Modified.values
0 aa_bb_25_0_1 aa_bb_25_0_1
0 273 aa_bb_25_0_1
0 60 aa_bb_25_0_1
1 aa_bb_7_0_2 aa_bb_7_0_2
1 335 aa_bb_7_0_2
2 aa_bb_65_1_1 aa_bb_65_1_1
2 456 aa_bb_65_1_1
3 aa_bb_98_0_1 aa_bb_98_0_1
我尝试了以下代码,但它给出了与输入
完全相同的输出awk -F"\t" -v OFS="\t" '$3!="" {three=$2} $0!="" {$3=three; print $0}' test.tx
答案 0 :(得分:2)
试试这个单行(忽略标题行):
awk -F"\t" -v OFS="\t" 'NR==1{next}$2~/^aa_bb/{p=$2}{print $0,p}' file
答案 1 :(得分:0)
输出控件的awk和column
:
awk '{
print $0,($1!=p?(NR==1?"Modified.":"")$2:q)
if($1!=p) {
p=$1 # p is the previous group
q=$2
}
}' file | column -t
group Values Modified.Values
0 aa_bb_25_0_1 aa_bb_25_0_1
0 273 aa_bb_25_0_1
0 60 aa_bb_25_0_1
1 aa_bb_7_0_2 aa_bb_7_0_2
1 335 aa_bb_7_0_2
2 aa_bb_65_1_1 aa_bb_65_1_1
2 456 aa_bb_65_1_1
3 aa_bb_98_0_1 aa_bb_98_0_1