要更新第9列和第11列和第12列的第2列基准值的值

时间:2018-05-03 17:40:16

标签: awk

我正在使用下面的代码,但输出错误。你能不能让我知道这里有什么问题。

awk -F"|" -v OFS="|" '{
**if((substr($9,0,2)=="02") && ($11="INV")) {$2="SAP_OSR_INV_FEB"}**
if((substr($9,0,2)=="03") && ($11="INV")) {$2="SAP_OSR_INV_MAR"}
if((substr($9,0,2)=="04") && ($11="INV")) {$2="SAP_OSR_INV_APR"}
if((substr($9,0,2)=="05") && ($11="INV")) {$2="SAP_OSR_INV_MAY"}
if((substr($9,0,2)=="06") && ($11="INV")) {$2="SAP_OSR_INV_JUN"}
if((substr($9,0,2)=="07") && ($11="INV")) {$2="SAP_OSR_INV_JUL"}
if((substr($9,0,2)=="08") && ($11="INV")) {$2="SAP_OSR_INV_AUG"}
if((substr($9,0,2)=="09") && ($11="INV")) {$2="SAP_OSR_INV_SEP"}
if((substr($9,0,2)=="10") && ($11="INV")) {$2="SAP_OSR_INV_OCT"}
if((substr($9,0,2)=="11") && ($11="INV")) {$2="SAP_OSR_INV_NOV"}
if((substr($9,0,2)=="12") && ($11="INV")) {$2="SAP_OSR_INV_DEC"}
**if((substr($9,0,2)=="02") && ($12="EXPWT")) {$2="SAP_OSR_EXPORT_FEB"}**
if((substr($9,0,2)=="03") && ($12="EXPWT")) {$2="SAP_OSR_EXPORT_MAR"}
if((substr($9,0,2)=="04") && ($12="EXPWT")) {$2="SAP_OSR_EXPORT_APR"}
if((substr($9,0,2)=="05") && ($12="EXPWT")) {$2="SAP_OSR_EXPORT_MAY"}
if((substr($9,0,2)=="06") && ($12="EXPWT")) {$2="SAP_OSR_EXPORT_JUN"}
if((substr($9,0,2)=="07") && ($12="EXPWT")) {$2="SAP_OSR_EXPORT_JUL"}
if((substr($9,0,2)=="08") && ($12="EXPWT")) {$2="SAP_OSR_EXPORT_AUG"}
if((substr($9,0,2)=="09") && ($12="EXPWT")) {$2="SAP_OSR_EXPORT_SEP"}
if((substr($9,0,2)=="10") && ($12="EXPWT")) {$2="SAP_OSR_EXPORT_OCT"}
if((substr($9,0,2)=="11") && ($12="EXPWT")) {$2="SAP_OSR_EXPORT_NOV"}
if((substr($9,0,2)=="12") && ($12="EXPWT")) {$2="SAP_OSR_EXPORT_DEC"}
if((substr($9,0,2)=="02") && ($11="CR")) {$2="SAP_OSR_CR_FEB"}
if((substr($9,0,2)=="03") && ($11="CR")) {$2="SAP_OSR_CR_MAR"}
if((substr($9,0,2)=="04") && ($11="CR")) {$2="SAP_OSR_CR_APR"}
if((substr($9,0,2)=="05") && ($11="CR")) {$2="SAP_OSR_CR_MAY"}
if((substr($9,0,2)=="06") && ($11="CR")) {$2="SAP_OSR_CR_JUN"}
if((substr($9,0,2)=="07") && ($11="CR")) {$2="SAP_OSR_CR_JUL"}
if((substr($9,0,2)=="08") && ($11="CR")) {$2="SAP_OSR_CR_AUG"}
if((substr($9,0,2)=="09") && ($11="CR")) {$2="SAP_OSR_CR_SEP"}
if((substr($9,0,2)=="10") && ($11="CR")) {$2="SAP_OSR_CR_OCT"}
if((substr($9,0,2)=="11") && ($11="CR")) {$2="SAP_OSR_CR_NOV"}
if((substr($9,0,2)=="12") && ($12="CR")) {$2="SAP_OSR_CR_DEC"}
if((substr($9,0,2)=="02") && $12="CAN") {$2="SAP_OSR_CAN_FEB"}
if((substr($9,0,2)=="03") && $12="CAN") {$2="SAP_OSR_CAN_MAR"}
if((substr($9,0,2)=="04") && $12="CAN") {$2="SAP_OSR_CAN_APR"}
if((substr($9,0,2)=="05") && $12="CAN") {$2="SAP_OSR_CAN_MAY"}
if((substr($9,0,2)=="06") && $12="CAN") {$2="SAP_OSR_CAN_JUN"}
if((substr($9,0,2)=="07") && $12="CAN") {$2="SAP_OSR_CAN_JUL"}
if((substr($9,0,2)=="08") && $12="CAN") {$2="SAP_OSR_CAN_AUG"}
if((substr($9,0,2)=="09") && $12="CAN") {$2="SAP_OSR_CAN_SEP"}
if((substr($9,0,2)=="10") && $12="CAN") {$2="SAP_OSR_CAN_OCT"}
if((substr($9,0,2)=="11") && $12="CAN") {$2="SAP_OSR_CAN_NOV"}
if((substr($9,0,2)=="12") && $12="CAN") {$2="SAP_OSR_CAN_DEC"}}1' file1.txt > file2.txt

我想在第9列的前两位和第11列和第12列的值更新第2列值

输入文件:

SAP||||||||022018|08AAACT2438A1ZT|INV|EXPWT|262881626|2018-02-02||||10||||TVH -COCO - VAISHALI|1151040011|8|08|08||||||9984|EVD0|EVD COCO CDMA Material|||0.00|8.47|0.00|0.00|9.00|0.76|9.00|0.76|||||||||||1301312397||ZEVD|1210|262881626|||||0
SAP||||||||022018|08AAACT2438A1ZT|INV|EXPWT|262881627|2018-02-02||||10||||TVH -COCO - VAISHALI|1151040011|8|08|08||||||9984|EVD0|EVD COCO CDMA Material|||0.00|8.47|0.00|0.00|9.00|0.76|9.00|0.76|||||||||||1301312569||ZEVD|1210|262881627|||||0
SAP||||||||022018|08AAACT2438A1ZT|INV|TAX|262881628|2018-02-02||||10||||TVH -COCO - VAISHALI|1151040011|8|08|08||||||9984|EVD0|EVD COCO CDMA Material|||0.00|8.47|0.00|0.00|9.00|0.76|9.00|0.76|||||||||||1301312574||ZEVD|1210|262881628|||||0

当前错误的ouptut。 在第2列中获取" SAP_OSR_CAN_FEB"这是错误的,第11栏和第12栏中的值也更新了" CR"和" CAN"这不应该只是想要更新第2列中的值

SAP|**SAP_OSR_CAN_FEB**|||||||022018|08AAACT2438A1ZT|**CR**|**CAN**|262881626|2018-02-02||||10||||TVH -COCO - VAISHALI|1151040011|8|08|08||||||9984|EVD0|EVD COCO CDMA Material|||0.00|8.47|0.00|0.00|9.00|0.76|9.00|0.76|||||||||||1301312397||ZEVD|1210|262881626|||||0
SAP|**SAP_OSR_CAN_FEB**|||||||022018|08AAACT2438A1ZT|**CR**|**CAN**|262881627|2018-02-02||||10||||TVH -COCO - VAISHALI|1151040011|8|08|08||||||9984|EVD0|EVD COCO CDMA Material|||0.00|8.47|0.00|0.00|9.00|0.76|9.00|0.76|||||||||||1301312569||ZEVD|1210|262881627|||||0
SAP|**SAP_OSR_CAN_FEB**|||||||022018|08AAACT2438A1ZT|**CR**|**CAN**|262881628|2018-02-02||||10||||TVH -COCO - VAISHALI|1151040011|8|08|08||||||9984|EVD0|EVD COCO CDMA Material|||0.00|8.47|0.00|0.00|9.00|0.76|9.00|0.76|||||||||||1301312574||ZEVD|1210|262881628|||||0

预期的正确输出:

SAP|SAP_OSR_EXPORT_FEB|||||||022018|08AAACT2438A1ZT|INV|EXPWT|262881626|2018-02-02||||10||||TVH -COCO - VAISHALI|1151040011|8|08|08||||||9984|EVD0|EVD COCO CDMA Material|||0.00|8.47|0.00|0.00|9.00|0.76|9.00|0.76|||||||||||1301312397||ZEVD|1210|262881626|||||0
SAP|SAP_OSR_EXPORT_FEB|||||||022018|08AAACT2438A1ZT|INV|EXPWT|262881627|2018-02-02||||10||||TVH -COCO - VAISHALI|1151040011|8|08|08||||||9984|EVD0|EVD COCO CDMA Material|||0.00|8.47|0.00|0.00|9.00|0.76|9.00|0.76|||||||||||1301312569||ZEVD|1210|262881627|||||0
SAP|SAP_OSR_INV_FEB|||||||022018|08AAACT2438A1ZT|INV|TAX|262881628|2018-02-02||||10||||TVH -COCO - VAISHALI|1151040011|8|08|08||||||9984|EVD0|EVD COCO CDMA Material|||0.00|8.47|0.00|0.00|9.00|0.76|9.00|0.76|||||||||||1301312574||ZEVD|1210|262881628|||||0

简短适用的条件:

if ($11 ~ /^INV$) {$2="SAP_OSR_INV_FEB}  append FEB if first two digit of column 9 is 02 or MAR at end in column 2 if first two digit of column 9 is 03
if ($12 ~ /^EXPWT$) {$2="SAP_OSR_EXPORTS_FEB} append FEB if first two digit of column 9 is 02 or MAR at end in column 2 if first two digit of column 9 is 03
if ($11 ~ /^CR$) {$2="SAP_OSR_CR_FEB} append FEB if first two digit of column 9 is 02 or MAR at end in column 2 if first two digit of column 9 is 03
if ($12 ~ /^CAN$) {$2="SAP_OSR_CAN_FEB} append FEB if first two digit of column 9 is 02 or MAR at end in column 2 if first two digit of column 9 is 03

1 个答案:

答案 0 :(得分:4)

我的眼睛正在燃烧那种剪切粘贴编程。

awk -F"|" -v OFS="|" '
    BEGIN {
        mon["01"] = "JAN"; mon["04"] = "APR"; mon["07"] = "JUL"; mon["10"] = "OCT"
        mon["02"] = "FEB"; mon["05"] = "MAY"; mon["08"] = "AUG"; mon["11"] = "NOV"
        mon["03"] = "MAR"; mon["06"] = "JUN"; mon["09"] = "SEP"; mon["12"] = "DEC"
    }
    # criteria
    $11 == "INV"   { val = $11 }
    $12 == "EXPWT" { val = "EXPORT" }
    $11 == "CR"    { val = $11 }
    $12 == "CAN"   { val = $12 }
    # apply rules
    { $2 = "SAP_OSR_" val "_" mon[substr($9,0,2)] }
    1
' file

请注意第11列和第12列的顺序检查:根据您的“简短”标准,

  1. col 12中的“CAN”优先于col 11中的“CR”,
  2. 第11栏中的“CR”优先于第12栏中的“EXPWT”
  3. 第13栏中的
  4. “EXPWT”优先于第11栏中的“INV”
  5. 如果这些规则不正确,您必须更准确地使用您的标准。