为了扩展我之前的问题,我有另一种文件模式,我试图更改第一列的名称,范围从 seq1 到 seq20 (seq1-seq20) as seq1 类似地,范围从 seq21 到 seq60 (seq21-seq60)as seq2 。文件名为 file.txt,格式如下:
seq22 19301 20914 fill_color=green_a0
seq55 16726 18337 fill_color=green_a0
seq10 167934 169650 fill_color=green_a0
seq36 200621 202367 fill_color=red_a0
seq7 160164 161903 fill_color=green_a0
seq56 31356 33104 fill_color=green_a0
seq25 15030 16656 fill_color=green_a0
seq43 99693 101326 fill_color=red_a0
seq19 66168 67689 fill_color=green_a0
seq50 55955 57479 fill_color=green_a0
seq9 454456 456277 fill_color=green_a0
seq35 282633 284453 fill_color=green_a0
seq10 354264 355872 fill_color=green_a0
seq36 10125 11742 fill_color=red_a0
seq3 106668 110910 fill_color=green_a0
输出文件看起来像
seq2 19301 20914 fill_color=green_a0
seq2 16726 18337 fill_color=green_a0
seq1 167934 169650 fill_color=green_a0
seq2 200621 202367 fill_color=red_a0
seq1 160164 161903 fill_color=green_a0
seq2 31356 33104 fill_color=green_a0
seq2 15030 16656 fill_color=green_a0
seq2 99693 101326 fill_color=red_a0
seq1 66168 67689 fill_color=green_a0
seq2 55955 57479 fill_color=green_a0
seq1 454456 456277 fill_color=green_a0
seq2 282633 284453 fill_color=green_a0
seq1 354264 355872 fill_color=green_a0
seq2 10125 11742 fill_color=red_a0
seq1 106668 110910 fill_color=green_a0
我尝试过这个
sed -e "s/seq[1:20]*/seq1/" -e "s/seq[21:60]*/seq2/" file.txt
和
awk 'NR>=seq1 && NR<=seq20{sub("seq*","seq1",$0)} 1' file.txt
答案1
我建议:
awk '{gsub(/[^0-9]/,"",$1); if($1+0<21){$1="seq1"} else {$1="seq2"}; print}' file
gsub(/[^0-9]/,"",$1)
删除第一列中除数字以外的所有内容。
答案2
您的描述和尝试看起来都很奇怪,但考虑到输入和所需的输出,我认为您想要这样:
$ awk '(NR%2==1) ? $1="seq2" :$1="seq1"' file.txt
seq2 19301 20914 fill_color=green_a0
seq1 16726 18337 fill_color=green_a0
seq2 167934 169650 fill_color=green_a0
seq1 200621 202367 fill_color=red_a0
seq2 160164 161903 fill_color=green_a0
seq1 31356 33104 fill_color=green_a0
seq2 15030 16656 fill_color=green_a0
seq1 99693 101326 fill_color=red_a0
seq2 66168 67689 fill_color=green_a0
seq1 55955 57479 fill_color=green_a0
seq2 454456 456277 fill_color=green_a0
seq1 282633 284453 fill_color=green_a0
seq2 354264 355872 fill_color=green_a0
seq1 10125 11742 fill_color=red_a0
seq2 106668 110910 fill_color=green_a0