replace columns UNIX with awk

前端 未结 1 701
伪装坚强ぢ
伪装坚强ぢ 2020-12-12 08:08

I want to replace data in spcific column that the user gives with id, with a value that again is given by the user. So the user gives the id of the row, the column, and the

相关标签:
1条回答
  • 2020-12-12 08:51

    You code does not match your requirement at all:

    the user gives the id of the row, the column, and the value to replace

    which I would interpret with

    awk -v row=2 -v col=3 -v new_value="Hello World" '
        BEGIN { FS = OFS = "|" }
        NR == row {$col = new_value}
        {print}
    ' persons.dat.txt
    

    Note the use of variables here:

    • an unadorned variable is like C, replace with the variable's value (usage of the "row" variable
    • the $ symbol is like an operator that references the value of the field number given by the value (here, "$col")
    0 讨论(0)
提交回复
热议问题