【问题标题】:Unix awk pass variable as fileNameUnix awk 将变量作为文件名传递
【发布时间】:2018-09-15 03:57:27
【问题描述】:

我正在使用以下命令将一个文件一分为二。

currentCouponFile="$DIR/kozo_agency_current_coupon_ts-"$HMD_EFF_DT".csv"
agencySpreadFile="$DIR/kozo_agency_spread_ts-"$HMD_EFF_DT".csv"

awk -F '|' 'BEGIN{OFS=","};{$1=$1; if(NF == 2){print > "$currentCouponFile"}else{ print > "$agencySpreadFile"}}' $fileName

在currentCouponFile 和agencySpreadFile 上回显给我想要的文件名。

但是,生成的文件被创建为$agencySpreadFile 和$currentCouponFile。不是 .csv 格式的。

有什么办法吗?

【问题讨论】:

    标签: file unix


    【解决方案1】:

    不要在 awk 中使用 shell 变量。

    currentCouponFile="$DIR/kozo_agency_current_coupon_ts-"$HMD_EFF_DT".csv"
    agencySpreadFile="$DIR/kozo_agency_spread_ts-"$HMD_EFF_DT".csv"
    
        awk -F '|' -v current="$currentCouponFile" -v agency="$agencySpreadFile" '
           BEGIN{OFS=","}
           {
           $1=$1;
           if(NF == 2) 
              {print > current}
           else
              {print > agency}
           }' $fileName
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-20
      • 1970-01-01
      • 2022-01-15
      • 1970-01-01
      • 2021-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多