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

Single and Double Quote Issue using GAWK

我在 Windows

中得到了以下 GAWK 命令行的帮助
gawk "{a = gensub(/([0-9]{6} [0-9]{6} [0-9]{4}).*itemId=, "\2 \1", "g", [=10=]); 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 中某个项目的信息,这些可以分布在整个文件中,我想获取第一个日期和时间以及最后一个日期和时间并创建一个像这样的行:

项目编号 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 命令在工作,但它们使用 " 双引号 谁能帮忙

谢谢

菲尔

通过 Windows 命令行正确获取引号和其他特殊字符通常很困难,尤其是对于像 gawk 这样为 Unix 设计的程序,因为有几种不同的方法可以移植基于 Unix 或其他非 Windows C 程序到 Windows.

上 C(或 C++)的多个细微不同的实现

对于 awk(和 perl)来说通常更好的方法是将 script 放在一个文件中,就像这样,它还允许您使用换行符来格式化它更好:

myscript.awk:

{a = gensub(/([0-9]{6} [0-9]{6} [0-9]{4}).*itemId=, "\2 \1", "g", [=10=]); 
 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 分支中的那个)有两个捕获组,但只使用一个。请与 'helped' 任何人联系。