使用 sed 准备更新命令

使用 sed 准备更新命令

我有一个包含 3 个不同列数据的输出文件 -

          ['AARF'],SAMPLE12,2016-01-05 12:00:00-0500
                  ,529OFFST,2015-04-16 08:04:21-0400
          "['EPROSP_IWS', '648099_EPROSP_IWS']",4.NDR-IWS-EPRO,2015-04-16 08:04:21-0400

我尝试使用下面的 sed 命令-

        sed -i "s/\(\"*\[[^]]*\]\"*\)\(.*\)/{\1:\"\"}\2/" tempFile
        sed "s/' *, *'/' '/g;s/\([^,]*\),\([^,]*\),\(.*\)/update table set cross_refs = \1 where id = \2 and effective_date = \3/;s/' '/','/g" tempFile > updatestmt.cql
        sed -i "s/$/';/" updatestmt.cql

我的预期o/p-

       update table set cross_refs ={'AARF':''}  where id = 'SAMPLE12' and effective_date = '2016-01-05 12:00:00-0500';
       update table set cross_refs = {'':''}  where id = '529OFFST' and effective_date = '22016-01-05 12:00:00-0500';
       update table set cross_refs = {'EPROSP_IWS':'','648099_EPROSP_IWS':''} where id = '4.NDR-IWS-EPRO' and effective_date = '2015-04-16 08:04:21-0400';

有人能帮我解决这个问题吗?

答案1

好吧,你可以这样做awk

BEGIN { FS="," }
{
    date=$NF
    id=$(NF-1)
    sub(/^ */, "", $1)
    sub(/"?\[/, "", $1)
    sub(/\]"?/, "", $(NF-2))
    ref = $1
    for (i=2; i < NF-1; i++) {
        ref = ref ":''," $(i)
    }
    if (!ref) { ref = "''" }
    print "update table set cross_refs={" ref ":''} where id='" id "' and effective_date = '" date "';\
"
}

相关内容