有一个文件,如下,以空格分成3段(每段是7行),我现在想这样从里面抽取数据,如果某段中含有字段“REPORT COUNT= 22985”(如果含有的话,肯定是在这段的第6行中含有),则把这一段数据都放到一个新文件中,该如何实现呢??
代码: |
YTCMSC01 AMA112 MAR07 15:19:01 2900 INFO LONG DURATION CALL ORIG= CDMA_CPS 000000 TERM= CLG NO= 13355356234 CLD NO= $ CONNECT TIME= 066/1426170 REPORT COUNT= 22986 ANSWERED= Y YTCMSC01 AMA112 MAR07 15:19:01 2800 INFO LONG DURATION CALL ORIG= CKT YTIGW1TRKE 477 TERM= CKT SB0014VOICE 121 CLG NO= 13854829695 CLD NO= 13361321136 CONNECT TIME= 066/1435147 REPORT COUNT= 22985 ANSWERED= Y YTCMSC01 AMA112 MAR07 15:19:01 2700 INFO LONG DURATION CALL ORIG= CDMA_CPS 000000 TERM= CLG NO= 13356916813 CLD NO= $ CONNECT TIME= 066/0849074 REPORT COUNT= 22984 ANSWERED= Y |
解决办法:
1.grep 'REPORT COUNT= 22985' -B5 -C1 youfile >newfile
2.cat $scr_file|awk 'BEGIN~/REPORT COUNT= 22985/ &print >'$dest_file'}'
3.awk 'BEGIN/REPORT COUNT= 22985/{print >"newfile"}' file
4.awk ' { for (i=7;i>0;i--);a[1]=;if (a[2]~/REPORT COUNT= 22986/){for (i=7;i>0;i--){print a[i]}}}' yourfile >newfile