РЕДАКТИРОВАТЬ3: Еще одна попытка, поскольку 2-й набор из 6 строк имеет 2 0 2 0 0 2
, поэтому его вывод должен быть 1 2, 1 2, 0 0,1 2
, если это так (что, я считаю, в идеале должно быть ), затем попробуйте выполнить следующее.
awk '
{
occur++
}
{
count=$0!=0?++count:count
sum+=$0
}
$0==0 || occur==6{
printf("%d %0.2f\n",count,count?sum/count:prev)
prev=count?sum/count:0
prev_count=count
count=sum=prev=prev_count=""
if(occur==6){
occur=""
}
}
END{
if(occur){
printf("%d %0.2f\n",count?count:prev_count,count?sum/count:prev)
}
}
' Input_file | awk '$1 != 0'
Вывод будет следующим:
6 5.33
1 2.00
1 2.00
1 2.00
5 4.20
1 3.00
3 4.00
РЕДАКТИРОВАТЬ, приведенные ниже, могут помочь в подобных проблемах которые немного отличаются от этой актуальной проблемы, поэтому держите их здесь в посте.
EDIT2: В случае, если вы не хотите СБРОС СЧЕТА всякий раз, когда в Input_file встречается ноль, затем попробуйте следующее Это будет постоянно искать только 6 строк и НЕ будет сбрасывать счет.
awk '
{
occur++
}
$0!=0{
count++
sum+=$0
found=prev_count=prev=""
}
$0==0 && occur!=6{
printf("%d,%0.2f\n",count?count:prev_count,count?sum/count:prev)
prev=count?sum/count:0
prev_count=count
count=sum=""
found=1
next
}
occur==6{
printf("%d,%0.2f\n",count,count?sum/count:prev)
prev=count?sum/count:0
prev_count=count
count=sum=occur=""
found=1
}
END{
if(!found){
printf("%d,%0.2f\n",count?count:prev_count,count?sum/count:prev)
}
}
' Input_file
РЕДАКТИРОВАТЬ1: Не могли бы вы попробовать следующее, протестировано и написано только с предоставленными образцами.
awk '
{
occur++
}
$0!=0{
count++
sum+=$0
found=prev_count=prev=""
}
$0==0{
printf("%d,%0.2f\n",count?count:prev_count,count?sum/count:prev)
prev=count?sum/count:0
prev_count=count
count=sum=occur=""
found=1
next
}
occur==6{
printf("%d,%0.2f\n",count,count?sum/count:prev)
prev=count?sum/count:0
prev_count=count
count=sum=occur=""
found=1
}
END{
if(!found){
printf("%d,%0.2f\n",count?count:prev_count,count?sum/count:prev)
}
}
' Input_file
О чем заботится код:
- Он заботится о логи c, где, если любые непрерывные 2 строки имеют значение
0
, тогда будут напечатаны предыдущие счетные и средние значения для этой строки. Это также позаботится о крайних случаях, таких как:
a- Если строка НЕ заканчивается 0
, она проверит, есть ли какие-либо значения для печати с помощью созданного мной found
флага.
b- В случае последнего входного_файла строка НЕ делится на 6, тогда и этот случай будет охватываться логикой END блока c проверки ее флагом found
.
Объяснение: Добавление подробного пояснения к приведенному выше коду.
awk ' ##Starting awk program from here.
{
occur++
}
$0!=0{ ##Checking condition if a line is NOT having zero value then do following.
count++ ##Increment variable count with 1 each time it comes here.
sum+=$0 ##Creating variable sum and keep adding current line value in it.
found=prev_count=prev="" ##Nullifying variables found, prev_count, prev here.
} ##Closing BLOCK for condition $0!=0 here.
$0==0{ ##Checking condition if a line is having value zero then do following.
printf("%d,%0.2f\n",count?count:prev_count,count?sum/count:prev) ##Printing count and count/sum here, making sure later is NOT getting divided by 0 too.
prev=count?sum/count:0 ##Creating variable prev which will be sum/count or zero in case count variable is NULL.
prev_count=count ##Creating variable prev_count whose value is count.
count=sum=occur="" ##Nullify variables count and sum here.
found=1 ##Setting value 1 to variable found here.
next ##next will skip all further statements from here.
} ##Closing BLOCK for condition $0==0 here.
occur==6{ ##Checking if current line is fully divided with 6 then do following.
printf("%d,%0.2f\n",count,count?sum/count:prev) ##Printing count and count/sum here, making sure later is NOT getting divided by 0 too.
prev=count?sum/count:0 ##Creating variable prev which will be sum/count or zero in case count variable is NULL.
prev_count=count ##Creating variable prev_count whose value is count.
count=sum=occur="" ##Nullifying variables count and sum here.
found=1 ##Setting value 1 to variable found here.
} ##Closing BLOCK for condition FNR%6==0 here.
END{ ##Starting END block for this awk program here.
if(!found){ ##Checking condition if variable found is NULL then do following.
printf("%d,%0.2f\n",count?count:prev_count,count?sum/count:prev) ##Printing count and count/sum here, making sure later is NOT getting divided by 0 too.
}
}
' Input_file ##Mentioning Input_file name here.