首先解释一下awk合并文件用到的变量,NR表示读到的文件行数,会对所有文件进行累加,FNR表示读到的当前文件行数,每次读完一个文件后就会从1重新开始。
文件1.request.txt(空格分隔,一共3列)
/game/bag/batchSell 98.5775 842
/game/bag/gainVipDailyGift 24.0069 785
/game/bag/open 9.54098 16
/game/bag/refresh 76.3525 820
/game/bag/useItem 67.3278 798
/game/banquet/newBanquet 91.8188 985[color=red][/color]
/game/battle/autoCheckPoint 111.041 4419
/game/battle/cancelOffLine 154.037 2120
/game/battle/checkpoint 267.485 8589
/game/battle/detect 343.987 844
/game/battle/offLineAuto 6.31881 49
/game/battle/speedUp 71.5616 2566
/game/beauty/addToGeneral 277.299 1068
文件2.requestShort.txt(空格分隔,一共2列)
/game/bag/batchSell 12
/game/bag/gainVipDailyGift 6
/game/bag/open 6
/game/bag/refresh 0
/game/bag/useItem 6
/game/banquet/newBanquet 20
/game/battle/autoCheckPoint 0
/game/battle/cancelOffLine 6
/game/battle/checkpoint 11
/game/battle/detect 2
/game/battle/offLineAuto 0
/game/battle/speedUp 0
/game/beauty/addToGeneral 7
要求:按照第一列进行合并,合并的命令如下:
awk 'NR==FNR{a[$1]=$2}NR>FNR&&a[b=$1]{print $0, a[b]}' request.log requestShort.log >result.txt
合并后的结果:
/game/bag/batchSell 98.5775 842 12
/game/bag/gainVipDailyGift 24.0069 785 6
/game/bag/open 9.54098 16 6
/game/bag/useItem 67.3278 798 6
/game/banquet/newBanquet 91.8188 985 20
/game/battle/cancelOffLine 154.037 2120 6
/game/battle/checkpoint 267.485 8589 11
/game/battle/detect 343.987 844 2
/game/beauty/addToGeneral 277.299 1068 7
文件1.request.txt(空格分隔,一共3列)
/game/bag/batchSell 98.5775 842
/game/bag/gainVipDailyGift 24.0069 785
/game/bag/open 9.54098 16
/game/bag/refresh 76.3525 820
/game/bag/useItem 67.3278 798
/game/banquet/newBanquet 91.8188 985[color=red][/color]
/game/battle/autoCheckPoint 111.041 4419
/game/battle/cancelOffLine 154.037 2120
/game/battle/checkpoint 267.485 8589
/game/battle/detect 343.987 844
/game/battle/offLineAuto 6.31881 49
/game/battle/speedUp 71.5616 2566
/game/beauty/addToGeneral 277.299 1068
文件2.requestShort.txt(空格分隔,一共2列)
/game/bag/batchSell 12
/game/bag/gainVipDailyGift 6
/game/bag/open 6
/game/bag/refresh 0
/game/bag/useItem 6
/game/banquet/newBanquet 20
/game/battle/autoCheckPoint 0
/game/battle/cancelOffLine 6
/game/battle/checkpoint 11
/game/battle/detect 2
/game/battle/offLineAuto 0
/game/battle/speedUp 0
/game/beauty/addToGeneral 7
要求:按照第一列进行合并,合并的命令如下:
awk 'NR==FNR{a[$1]=$2}NR>FNR&&a[b=$1]{print $0, a[b]}' request.log requestShort.log >result.txt
合并后的结果:
/game/bag/batchSell 98.5775 842 12
/game/bag/gainVipDailyGift 24.0069 785 6
/game/bag/open 9.54098 16 6
/game/bag/useItem 67.3278 798 6
/game/banquet/newBanquet 91.8188 985 20
/game/battle/cancelOffLine 154.037 2120 6
/game/battle/checkpoint 267.485 8589 11
/game/battle/detect 343.987 844 2
/game/beauty/addToGeneral 277.299 1068 7