我有这个数据:

111,24,1,\N,\N,[],aa
222,25,2,\N,\N,[],bb
333,26,3,\N,\N,[],\N

我想用\N替换第4列和第5列中的所有awk值。
我写了这封信,但不确定我是否走对了路:
awk -F "," '{print $1 "," $2 "," $3 "," $4/ "," $5/ "," $6 "," $7}'

这个解决方案是不灵活的。如果我有100列呢?
更新
预期产量:
111,24,1,4.3,2.1,[],aa
222,25,2,4.3,2.1,[],bb
333,26,3,4.3,2.1,[],\N

最佳答案

我会写:

awk '
    BEGIN {FS = OFS = ","}
    $4 == "\\N" {$4 = 4.3}
    $5 == "\\N" {$5 = 2.1}
    {print}
' file

或者,如果你的案子更复杂
awk '
    BEGIN {
        FS = OFS = ","
        repl[4] = 4.3
        repl[5] = 2.1
        repl[7] = "hello"
    }
    {for (i in repl) if ($i == "\\N") $i = repl[i]}
    {print}
' file

关于linux - 如何替换csv所有列中的特定值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45217576/

10-14 03:23