我有一个带有开始号和结束号的文件,如下所示
Start,end,code
43786,67883,avb
200,400,add
12,14,adf
然而,我需要重写它,以便开始和结束之间的所有数字都用它们的代码编写:
43786,Avb
43787,avb
43788,avb
43789,avb
直到最后67883,avb在该范围内并继续
200,add
201,add
202,add
答案1
像这样的东西应该足够了:
awk 'BEGIN{FS=OFS=","}{for(i=$1;i<=$2;i++) print i,$3}' input_file > output_file
如果您的文件有不需要打印的标题,则:
awk 'BEGIN{FS=OFS=","}NR>1{for(i=$1;i<=$2;i++) print i,$3}' input_file > output_file
答案2
perl -F, -lane '$. > 1 and print "$_,$F[2]" for $F[0] .. $F[1]'
perl -F, -lane '$. == 1 && next, print "$a,$F[2]" while ($a=$F[0]++) <= $F[1]'
while IFS=, read -r start end str junk; do
case ${v++} in '' ) v=; continue ;; esac
seq -f "%g,$str" $start $end
done
while IFS=, read -r start end str junk; do
case ${v++} in '' ) v=; continue ;; esac
yes "$str" | sed -e "$start,$end!d;=;${end}q" | sed -e 'N;s/\n/,/'
done