使用GAWK的单引号和双引号问题

时间:2017-08-27 19:32:49

标签: quotes gawk

我在Windows中获得了以下GAWK命令行的帮助

gawk "{a = gensub(/([0-9]{6} [0-9]{6} [0-9]{4}).*itemId=, "\\2 \\1", "g", $0); b = split(a, c, " "); if (c[1] in result) result[c[1]] = gensub(/(.+),(.+)/, "\\1," c[2] " " c[3] " " c[4], "g", result[c[1]]); else result[c[1]] = c[2] " " c[3] " " c[4] "," c[2] " " c[3] " " c[4]} END { for (i in result) print i ": " result[i]}" Item.log 

我有成千上万行文本,如下所示,其中包含ITEMID中项目的信息,这些可以传播到整个文件中,我希望得到第一个日期和时间以及最后的日期和时间,并创建一个像:

ITEMID 170209 003122 170209 003621

日期格式约为YYMMDD HHMMSS

170209 003122 0893 DE(N) ItemHandler.ItemLog event=<VOLUME_RESULT>, *************************, itemId=<12551677>, globalId=<12551677>, cmcIndex=<973>, sorter=<0: MS01>, state=<CSC: WaitForData>, volumeId=<4104: MS01.IU05-VMS04>, volumeStatus=<0>, volScanResult=<0>, length=<665 mm>, width=<610 mm>, height=<470 mm>, volume=<190656000 mm3>, position=<sorter#0.volume#4104: MS01.IU05-VMS04>, volSystemId=<4>, volReferenceRecord=<2542>, volLevelOffset=<0>, volDataCrc=<48107>, itemRevisionNumber=<3> ##[
170209 003126 0210 DE(N) ItemHandler.ItemLog event=<SCANNER_RESULT>, ************************, itemId=<12551674>, globalId=<12551674>, cmcIndex=<970>, sorter=<0: MS01>, state=<CSC: WaitForData>, barcodeCount=<1>, barcodes=<[ProxyWrapperBarcode(barcode=<T000042357>, type=<C0>, result=<OK>, ccType=<>)]>, codeSource=<ohscan>, scannerId=<4000: SCAN01>, scannerStatus=<0>, position=<sorter#0.scanner#4000: SCAN01>, itemRevisionNumber=<5> ##[
170209 003126 0210 DE(N) ItemHandler.ItemLog event=<DESTINATION_REQUEST>, *******************, itemId=<12551674>, globalId=<12551674>, cmcIndex=<970>, sorter=<0: MS01>, state=<CSC: WaitForData>, barcodes=<[ProxyWrapperBarcode(barcode=<T000042357>, type=<C0>, result=<OK>, ccType=<>)]>, ccReason=<SCANNER_DATA_ADDED>, PreviousccResult=<>, sortSchemeId=<-1>, sortSchemeName=<>, logicalDestination=<>, BatchCountItem=<true>, collectionId=<-1>, goodsId=<>, position=<sorter#0.scanner#4000: SCAN01>, dynamicDataCount=<0>, dynamicData=<{}>, carrierId=<202>, carrierCount=<1>, itemRevisionNumber=<5> ##[
170209 003126 0225 DE(N) ItemHandler.ItemLog event=<DESTINATION_REPLY>, *********************, itemId=<12551674>, globalId=<12551674>, cmcIndex=<970>, sorter=<0: MS01>, state=<CSC: WaitForDestination>, ccReason=<SCANNER_DATA_ADDED>, PendingccResult=<OK>, Pendingstrategy=<notSpecified>, PendingchuteGroup=<[3000]: Parked0>, PendingNotChutedestinationId=<-1>, PendingsortSchemeId=<-1>, PendingsortSchemeName=<>, PendinglogicalDestination=<>, PendinggoodsId=<>, PendingBatchCountItem=<true>, PendingcollectionId=<-1>, position=<sorter#0.scanner#4000: SCAN01>, dynamicDataCount=<0>, dynamicData=<{}>, itemRevisionNumber=<7> ##[
170209 003126 0225 DE(N) ItemHandler.ItemLog event=<REDIRECT_ITEM>, *************************, itemId=<12551674>, globalId=<12551674>, cmcIndex=<970>, sorter=<0: MS01>, state=<CSC: WaitForDestination>, PendingchuteGroup=<[3000]: Parked0>, Pendingstrategy=<notSpecified>, CscdestinationId=<-1: UnDef>, CmcdestinationId=<4099: All Scanners>, position=<sorter#0.scanner#4000: SCAN01>, itemRevisionNumber=<7> ##[
170209 003126 0454 DE(N) ItemHandler.ItemLog event=<REDIRECT_ITEM_REPLY>, *******************, itemId=<12551674>, globalId=<12551674>, cmcIndex=<970>, sorter=<0: MS01>, state=<CSC: WaitForDestination>, CscdestinationId=<3000: Parked0>, chuteGroup=<[3000]: Parked0>, CmcdestinationId=<3000: Parked0>, position=<sorter#0.scanner#4000: SCAN01>, chuteListStartPoint=<-1>, itemRevisionNumber=<9> ##[
170209 003126 0456 DE(N) ItemHandler.ItemLog event=<RECONVERT>, *****************************, itemId=<12551674>, globalId=<12551674>, cmcIndex=<970>, sorter=<0: MS01>, state=<CSC: WaitForData>, CscdestinationId=<3000: Parked0>, chuteGroup=<[3000]: Parked0>, CmcdestinationId=<3000: Parked0>, position=<sorter#0.scanner#4000: SCAN01>, chuteListStartPoint=<-1>, itemRevisionNumber=<10> ##[
170209 003126 0456 DE(N) ItemHandler.ItemLog event=<DESTINATION_REQUEST>, *******************, itemId=<12551674>, globalId=<12551674>, cmcIndex=<970>, sorter=<0: MS01>, state=<CSC: WaitForData>, barcodes=<[ProxyWrapperBarcode(barcode=<T000042357>, type=<C0>, result=<OK>, ccType=<>)]>, ccReason=<SCANNER_DATA_ADDED>, PreviousccResult=<OK>, sortSchemeId=<-1>, sortSchemeName=<>, logicalDestination=<>, BatchCountItem=<true>, collectionId=<-1>, goodsId=<>, position=<sorter#0.scanner#4000: SCAN01>, dynamicDataCount=<0>, dynamicData=<{}>, carrierId=<202>, carrierCount=<1>, itemRevisionNumber=<10> ##[
170209 003126 0458 DE(N) ItemHandler.ItemLog event=<DESTINATION_REPLY>, *********************, itemId=<12551674>, globalId=<12551674>, cmcIndex=<970>, sorter=<0: MS01>, state=<CSC: WaitForDestination>, ccReason=<SCANNER_DATA_ADDED>, PendingccResult=<OK>, Pendingstrategy=<priority>, PendingchuteGroup=<[186, 188, 195, 201, 184]: [XRA502, XRA503, XRA605, XRA607, XRA501]>, PendingNotChutedestinationId=<-1>, PendingsortSchemeId=<-1>, PendingsortSchemeName=<>, PendinglogicalDestination=<XRA502>, PendinggoodsId=<>, PendingBatchCountItem=<true>, PendingcollectionId=<-1>, position=<sorter#0.scanner#4000: SCAN01>, dynamicDataCount=<0>, dynamicData=<{}>, itemRevisionNumber=<12> ##[
170209 003126 0458 DE(N) ItemHandler.ItemLog event=<REDIRECT_ITEM>, *************************, itemId=<12551674>, globalId=<12551674>, cmcIndex=<970>, sorter=<0: MS01>, state=<CSC: WaitForDestination>, PendingchuteGroup=<[186, 188, 195, 201, 184]: [XRA502, XRA503, XRA605, XRA607, XRA501]>, Pendingstrategy=<priority>, CscdestinationId=<3000: Parked0>, CmcdestinationId=<3000: Parked0>, position=<sorter#0.scanner#4000: SCAN01>, itemRevisionNumber=<12> ##[
170209 003126 0681 DE(N) ItemHandler.ItemLog event=<REDIRECT_ITEM_REPLY>, *******************, itemId=<12551674>, globalId=<12551674>, cmcIndex=<970>, sorter=<0: MS01>, state=<CSC: WaitForDestination>, CscdestinationId=<186: XRA502>, chuteGroup=<[186, 188, 195, 201, 184]: [XRA502, XRA503, XRA605, XRA607, XRA501]>, CmcdestinationId=<186: XRA502>, position=<sorter#0.scanner#4000: SCAN01>, chuteListStartPoint=<186>, itemRevisionNumber=<14> ##[
170209 003126 0981 DE(N) ItemHandler.ItemLog event=<ITEM_INDUCTED>, *************************, itemId=<12551677>, globalId=<12551677>, cmcIndex=<973>, sorter=<0: MS01>, state=<CSC: WaitForData>, inductionId=<4: IU05>, inductionMode=<SCANNER>, inductStatus=<NORMAL_ITEM>, carrierId=<343>, carrierCount=<1>, CmcdestinationId=<4099: All Scanners>, position=<sorter#0: MS01>, itemRevisionNumber=<4> ##[
170209 003129 0845 DE(N) ItemHandler.ItemLog event=<ITEM_AT_INDUCTION>, *********************, itemId=<12551678>, globalId=<12551678>, cmcIndex=<974>, sorter=<0: MS01>, state=<CSC: ProjectIdle>, inductionId=<4: IU05>, position=<sorter#0.induction#4: IU05>, itemRevisionNumber=<0> ##[
170209 003129 0845 DE(N) ItemHandler.ItemLog event=<SET_ITEM_ID>, ***************************, itemId=<12551678>, globalId=<12551678>, cmcIndex=<974>, sorter=<0: MS01>, state=<CSC: ProjectIdle>, itemRevisionNumber=<0> ##[
170209 003130 0027 DE(N) ItemHandler.ItemLog event=<ITEM_PASSED_SPS>, ***********************, itemId=<12551665>, globalId=<12551665>, cmcIndex=<961>, sorter=<0: MS01>, state=<CSC: ProjectHeadingForChute>, carrierId=<363>, carrierCount=<1>, CmcdestinationId=<198: EXP507>, CscdestinationId=<198: EXP507>, itemRevisionNumber=<15> ##[
170209 003130 0350 DE(N) ItemHandler.ItemLog event=<DISCHARGE_ATTEMPTED>, *******************, itemId=<12551665>, globalId=<12551665>, cmcIndex=<961>, sorter=<0: MS01>, state=<CSC: ProjectHeadingForChute>, CscdestinationId=<198: EXP507>, chuteGroup=<[198]: EXP507>, CmcdestinationId=<198: EXP507>, position=<sorter#0: MS01>, itemRevisionNumber=<15> ##[
170209 003130 0352 DE(N) ItemHandler.ItemLog event=<RECONVERT>, *****************************, itemId=<12551665>, globalId=<12551665>, cmcIndex=<961>, sorter=<0: MS01>, state=<CSC: ProjectHeadingForChute>, CscdestinationId=<198: EXP507>, chuteGroup=<[198]: EXP507>, CmcdestinationId=<198: EXP507>, position=<sorter#0: MS01>, chuteListStartPoint=<198>, itemRevisionNumber=<16> ##[
170209 003130 0352 DE(N) ItemHandler.ItemLog event=<DESTINATION_REQUEST>, *******************, itemId=<12551665>, globalId=<12551665>, cmcIndex=<961>, sorter=<0: MS01>, state=<CSC: ProjectHeadingForChute>, barcodes=<[ProxyWrapperBarcode(barcode=<3709160626>, type=<A0>, result=<OK>, ccType=<>), ProxyWrapperBarcode(barcode=<2LIN400023+48000001>, type=<C0>, result=<OK>, ccType=<>)]>, ccReason=<SCANNER_DATA_ADDED>, PreviousccResult=<OK>, sortSchemeId=<-1>, sortSchemeName=<>, logicalDestination=<EXP507>, BatchCountItem=<true>, collectionId=<-1>, goodsId=<>, position=<sorter#0: MS01>, dynamicDataCount=<1>, dynamicData=<{VID=2017020905551665}>, carrierId=<363>, carrierCount=<1>, itemRevisionNumber=<16> ##[
170209 003130 0354 DE(N) ItemHandler.ItemLog event=<DESTINATION_REPLY>, *********************, itemId=<12551665>, globalId=<12551665>, cmcIndex=<961>, sorter=<0: MS01>, state=<CSC: WaitForDestination>, ccReason=<SCANNER_DATA_ADDED>, PendingccResult=<OK>, Pendingstrategy=<notSpecified>, PendingchuteGroup=<[3000]: Parked0>, PendingNotChutedestinationId=<-1>, PendingsortSchemeId=<-1>, PendingsortSchemeName=<>, PendinglogicalDestination=<>, PendinggoodsId=<>, PendingBatchCountItem=<true>, PendingcollectionId=<-1>, position=<sorter#0: MS01>, dynamicDataCount=<1>, dynamicData=<{VID=2017020905551665}>, itemRevisionNumber=<18> ##[
170209 003130 0354 DE(N) ItemHandler.ItemLog event=<REDIRECT_ITEM>, *************************, itemId=<12551665>, globalId=<12551665>, cmcIndex=<961>, sorter=<0: MS01>, state=<CSC: WaitForDestination>, PendingchuteGroup=<[3000]: Parked0>, Pendingstrategy=<notSpecified>, CscdestinationId=<198: EXP507>, CmcdestinationId=<198: EXP507>, position=<sorter#0: MS01>, itemRevisionNumber=<18> ##[
170209 003130 0517 DE(N) ItemHandler.ItemLog event=<REDIRECT_ITEM_REPLY>, *******************, itemId=<12551665>, globalId=<12551665>, cmcIndex=<961>, sorter=<0: MS01>, state=<CSC: WaitForDestination>, CscdestinationId=<3000: Parked0>, chuteGroup=<[3000]: Parked0>, CmcdestinationId=<3000: Parked0>, position=<sorter#0: MS01>, chuteListStartPoint=<198>, itemRevisionNumber=<20> ##[
170209 003130 0520 DE(N) ItemHandler.ItemLog event=<RECONVERT>, *****************************, itemId=<12551665>, globalId=<12551665>, cmcIndex=<961>, sorter=<0: MS01>, state=<CSC: WaitForData>, CscdestinationId=<3000: Parked0>, chuteGroup=<[3000]: Parked0>, CmcdestinationId=<3000: Parked0>, position=<sorter#0: MS01>, chuteListStartPoint=<198>, itemRevisionNumber=<21> ##[

我遇到的问题是,由于在DOS下的Windows批处理文件中运行单引号和双引号时出现问题,我无法使上述命令行正常工作,我还有其他gawk命令工作但是他们使用“双引号可以任何人帮助

由于

菲尔

1 个答案:

答案 0 :(得分:2)

通过Windows命令行正确获取引号和其他特殊字符通常很难,特别是对于像gawk这样的Unix程序,因为有几种不同的方法可以将基于Unix或非Windows C的程序移植到多个不同的实现中Windows上的C(或C ++)。

通常更好的awk(和perl)方法是将脚本放在一个文件中,类似这样,这也允许你使用换行符来更好地格式化它:

myscript.awk:

{a = gensub(/([0-9]{6} [0-9]{6} [0-9]{4}).*itemId=, "\\2 \\1", "g", $0); 
 b = split(a, c, " ");
 if (c[1] in result) result[c[1]] = gensub(/(.+),(.+)/, "\\1," c[2] " " c[3] " " c[4], "g", result[c[1]]);
 else result[c[1]] = c[2] " " c[3] " " c[4] "," c[2] " " c[3] " " c[4] } 
END { for (i in result) print i ": " result[i] }

然后

gawk -f myscript.awk item.log

PS:你的第一个gensub显然错过了/的结束itemId=,但即使有了这个改变也没什么意义,因为你只有一个捕获组,但你指定了一个替换使用两个的字符串。相反,您的第二个gensubif分支中的一个)有两个捕获组,但只使用一个。请与“帮助”你的人核实。