2016-05-12 89 views
0

我有一個包含3個不同的列中的輸出文件DATA-準備更新命令使用的sed

  ['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 /對 -

 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'; 

有人能幫助我嗎?

+4

這是否必須是'sed'? – cdarke

+0

@ cdarke-實際上是使用sed解析字符串。但沒有得到預期的輸出。你能否就此建議我? – saurav

+0

我剛剛更新了我的預期輸出結果。有人可以幫我解決問題 – saurav

回答

3

不在sed但逗號分隔的數據等於要求awk我:)

#!awk 
BEGIN { FS = "," } # split fields on comma 
{ 
    gsub(/[\]\[\"]/,"") # remove useless chars: [,]," 
    i=1 
    crefs="{" 
    while (i <= (NF-2)) { # loop from first to 3rd last field 
     tmp = $(i) ? $(i) : "''" # if no value, add empty quotes 
     crefs=crefs""tmp":''," # append current field value or empty to crefs 
     i++ 
    } 
    sub(/,$/, "", crefs) # remove trailing comma 
    crefs=crefs"}" 
    print "update table set cross_refs ="crefs" where id='"$(NF-1)"' and effective_date ='"$(NF)"'" 
} 
+0

非常感謝你 – saurav