AWK脚本

问题描述如下需要取得奇数行PlatformId:后边的字段或者insert后的第10个字段, 奇数行不会同时出现这两个标签

偶数行的竖线分割的第二个字段

数据如下


{"Channel":"abc","PlatformId":"123345","CpInfo":"","Code":"1","Message":""}
28202|1962|214290201||1427859800|xxxxxxxxxxxxxxxx
--
{"Channel":"abc","PlatformId":"123346","CpInfo":"","Code":"1","Message":""}
28202|1932|214290208||1427859979|xxxxxxxxxxxxxxx
--
insert into mylogin(platformId,tank,uuk,kku) values ('12347','214290210','357159050234338',1427860159);
28202|1969|214290210||1427860159|xxxxxxxxxxxxxxx
--
{"Channel":"abc","PlatformId":"123348","CpInfo":"","Code":"1","Message":""}
28202|1962|214290201||1427859800|xxxxxxxxxxxxxxxx
--
{"Channel":"abc","PlatformId":"123349","CpInfo":"","Code":"1","Message":""}
28202|1932|214290208||1427859979|xxxxxxxxxxxxxxx
--
insert into mylogin(platformId,tank,uuk,kku) values ('123450','214290210','357159050234338',1427860159);
28202|1969|214290210||1427860159|xxxxxxxxxxxxxxx
--




awk脚本如下文件名称awk-test.sh


#!/usr/bin/awk -f
BEGIN{#这里得大括弧不可以放在下边BEGIN必须要大写
	FS="[ \(,:|]";
	count=0 ;
	line=0;
	account="";	
}
#行号自加过滤掉无效的空白行
{
	line++; 
	if(NF >1) 
		count++
}
#在出现标签的行把需要的数据现保存在account变量
/PlatformId/{#这里得大括弧不可以放在下边
	account=$4
} 
/insert/{#这里得大括弧不可以放在下边
	account=$10
}
#在偶数行打印需要输出的数据
{
	if(count%2==0 && NF>1)
		printf("%s,%d,count:%d\n",account,$2,line)
}



执行方法 ./awk-test.sh a.txt 数据要保存在a.txt内


你可能感兴趣的:(AWK脚本)