16.awk根据条件求平均值
FFF;2011-10-1;2011-10-1 0:15 ;4.33278
FFF;2011-10-1;2011-10-1 0:00 ;4.57389
FFF;2011-10-2;2011-10-2 12:45;5.95446
FFF;2011-10-2;2011-10-2 15:00;6.21554
FFF;2011-10-3;2011-10-3 16:15;6.11111
FFF;2011-10-3;2011-10-3 14:45;6.21334
FFF;2011-10-3;2011-10-3 20:15;5.95223
FFF;2011-10-3;2011-10-3 8:30 ;4.05778
我要的结果,问题描述:求$1,$2相同的条件下,求$4的均值,并将均值放在每行的第$5,即
FFF;2011-10-1;2011-10-1 0:15 ;4.33278 ; 4.453335
FFF;2011-10-1;2011-10-1 0:00 ;4.57389 ; 4.453335
FFF;2011-10-2;2011-10-2 12:45;5.95446 ; 6.085
FFF;2011-10-2;2011-10-2 15:00;6.21554 ; 6.085
FFF;2011-10-3;2011-10-3 16:15;6.11111 ; 5.583615
FFF;2011-10-3;2011-10-3 14:45;6.21334 ; 5.583615
FFF;2011-10-3;2011-10-3 20:15;5.95223 ; 5.583615
FFF;2011-10-3;2011-10-3 8:30 ;4.05778 ; 5.583615
awk -F ';' 'BEGIN{while(getline<"file"){a[$1$2]+=$4;b[$1$2]++;avg[$1$2]=a[$1$2]/b[$1$2]}}a[$1$2]{print $0,avg[$1$2]}' file
awk -F';' 'BEGIN{while(getline<"file"){a[$1$2]+=$4;b[$1$2]++}}{$0=$0 FS a[$1$2]/b[$1$2]}1' file
awk -F ';' 'NR==FNR{a[$1$2]+=$4;b[$1$2]++;next}{print $0 ";" a[$1$2]/b[$1$2]}' file file
awk -vFS=';' '{a[$1$2]+=$4;c[$1$2]++;b[NR$1$2]=$0}END{for(i in b){for(j in a){if(match(i,j)){print b[i]";"a[j]/c[j]}}}}' file
17.指定多个空格或者多个;为分隔符
awk -F'[ ;]+' '{print $1}'
18.合并两个文件的列
cat a.txt
1 11
2 22
3 33
4 44
5 55
cat b.txt
a aa
b bb
c cc
d dd
e ee
希望合并成
1 a 11 aa
2 b 22 bb
3 c 33 cc
4 d 44 dd
5 e 55 ee
awk '{t=$0;getline <"file2";split(t,a," ");print a[1],$1,a[2],$2}' file1 OFS="\t"
19.
假设文本a.txt内容如下
abc
def
123
789
4556
abc
eeeeefff
123
5588
5454
abc
4444
5555
123
需将匹配段首abc段尾123的内容打印出来,目标结果如下:
abc
def
123
abc
eeeeefff
123
abc
4444
5555
123
遇到abc打开开关,遇到123关闭开关
awk '/abc/{i=1}i{print;if($0~/123/)i=0}' file
sed '/abc/{:a;N;/\n123/!ba;p};d' file
阅读(1160) | 评论(0) | 转发(0) |