2017-10-15 51 views
1

./tool.sh -f <file> --edit <id> <column> <value>awk中overwritting文件的列值給出腳本參數

必須執行和數據庫如

#id|lastName|firstName|gender|birthday|creationDate|locationIP|browserUsed 
933|Perera|Mahinda|male|1989-12-03|2010-03-17T13:32:10.447+0000|192.248.2.123|Firefox 
1129|Lepland|Carmen|female|1984-02-18|2010-02-28T04:39:58.781+0000|81.25.252.111|Internet Explorer 
4194|Do|Hα»^Ó ChΓ­|male|1988-10-14|2010-03-17T22:46:17.657+0000|103.10.89.118|Internet Explorer 
8333|Wang|Chen|female|1980-02-02|2010-03-15T10:21:43.365+0000|1.4.16.148|Internet Explorer 
8698|Liu|Chen|female|1982-05-29|2010-02-21T08:44:41.479+0000|14.103.81.196|Firefox 
8853|Monteno|Albin|male|1986-04-09|2010-03-19T21:52:36.860+0000|178.209.14.40|Internet Explorer 
10027|Chen|Ning|female|1982-12-08|2010-02-22T17:59:59.221+0000|1.2.9.86|Firefox 
1099511628908|Chen|Wei|female|1985-08-02|2010-05-24T20:52:26.582+0000|27.98.244.108|Firefox 
1099511633435|Smith|Jack|male|1981-04-19|2010-05-26T03:45:11.772+0000|50.72.193.218|Internet Explorer 
1099511635042|Kiss|Gyorgy|male|1984-09-14|2010-05-16T22:57:41.808+0000|91.137.244.86|Chrome 
1099511635218|Law-Yone|Eric|male|1987-01-20|2010-05-26T20:10:22.515+0000|203.81.95.235|Chrome 
1099511638444|Jasani|Chris|female|1981-05-22|2010-04-29T20:50:40.375+0000|196.223.11.62|Firefox 

並給予id列和值,改變該特定ID,該值列

ex。 ./tool.sh -f people.dat --edit 933 4 female

應覆蓋該文件 933|Mahinda|female|1989-12-03|2010-03-17T13:32:10.447+0000|192.248.2.123|Firefox

我的代碼是:

while [ $# -gt 0 ] 
    do 
      case $1 in 

      --edit) 
        id="$2"; 
        col="$3"; 
        val="$4"; 
      shift 3 
      ;; 
      -f) 
        dbfile=$2; 
      shift 
      esac 
      shift 
    done 

    egrep -o '^[^#]+' ${dbfile} | awk -F '|' -v OFS='|' -v id="${id}" -v col="${col}" -v val="${val}" '{if (($1="id") && ("col"<=8 && "col">=2)) {gsub($col,val)};{print}}' 

到目前爲止,該文件中沒有永久的變化,唯一的變化有,第一行的所有值成爲 「ID」

+1

不清楚替換你的最後一行,請你解釋一下代碼標籤中應該輸出什麼內容? – RavinderSingh13

+0

「到目前爲止,文件中沒有永久性更改。」許多unix工具不提供覆蓋現有文件的選項。 '* nix'中的安全成語是'awk'{program}'inFile> outFile &&/bin/mv outFile inFile'。祝你好運。 – shellter

+0

另外,學**使用http://shellcheck.net **之前**你的代碼發佈在這裏;-)。當你使用shellcheck時,你需要在第一行包括一個合適的「she-bang」行,通常是'#!/ bin/bash'。祝你好運。 – shellter

回答

1

你有

if (($1="id") && ("col"<=8 && "col">=2)) { 

    ^    ^
     |     | 
     |     | 
     |   Since you got string col, which will always evaluate false 
     | 
     | 
    you are assigning field1 ($1) with string "id" which will be true always 

如果你想申請可變一些條件,那麼你需要

if (($1==id) && ($col<=8 && $col>=2)) { 

    ^   ^
     |    | 
     |   AND column value corresponding to variable col is less 
     |   than or equal to 8 and greater than or equal to 2 
     |         ^
     |          | 
    if field1 is equal to variable id   | 
    ^         | 
     |          | 
     |          | 
     _____________ If both are true________| 
         | 
         | 

        gsub($col, $val) 

既然你有標題

在awk overwritting給出腳本參數

1文件的列值),而不是gsub($col, $val),您可以使用$col = $val

2)並且不需要egrep -o '^[^#]+' ${dbfile} |,因爲你有你的AWK $1==id && $col<=8 && $col>=2,這將照顧它,並可以如下簡化:

awk -F '|' -v OFS='|' -v id="${id}" -v col="${col}" -v val="${val}" ' 
     $1==id && $col<=8 && $col>=2 { $col = val; print} 
     ' "${dbfile}" 
+0

我用'egrep-o'^ [^#] +''忽略用# –

+0

啓動的任何註釋但是'awk -F'|' -v OFS ='|' -v id =「$ {id}」-v col =「$ {col}」-v val =「$ {val}」' $ 1 == id && $ col <=8 && $col> = 2 {$ col = val; print} '「$ {dbfile}」'沒有覆蓋文件。 –

+0

如果你正在使用'gawk',可以使用'gawk -i inplace',否則你必須寫入一些臨時文件,然後像''dbffile}> tmpfile && mv tmpfile「$ {dbfile}」那樣移動' –

0

通過這一個

sed -i '/'"$id"'/s/\([^|]*\)/'"$val"'/'"$col" "$dbfile" 
+0

實際上,它感謝你沒有得到'\([^ |] * \)'部分。你能不能詳細說明一下? –

+0

好吧,我試試:[^ |] *表示每個字符都不是'|',所以它是列的內容,因爲'|'單獨的列。這種模式匹配7次。我告訴sed將$ col列的內容替換爲$ val的值。請參閱info sed subtitute命令的「s」並標記g或數字。 –

+0

您必須替換'/'「$ id」'/ by'/ ^'「$ id」'| /以匹配$ id更安全。 –