Replace column values in a CSV file with awk

穿精又带淫゛_ 提交于 2019-12-04 18:40:37
awk -F ',' -v OFS=',' '$1 { if ($3=="") $3="No value"; print}' in.txt
  • Passes the field separator via the -F option.
  • Variable OFS, the output-field separator, is set to ,, so that output fields are also separated by ,.
  • Pattern $1 ensures that only non-empty lines are processed (that is, the associated action is only executed if the first field is non-empty) - if your input file has no empty lines, you can remove this pattern.
  • If the 3rd field is empty, it is assigned string "No value"
  • Finally, the line (with the potentially modified 3rd field) is output.

The above is how I suggest you approach the problem, but here are the problems with your original command:

  • {{FS=","}... Inside your single action - which due to not having a preceding pattern is executed for every input line - you set variable FS for every line - which is not only unnecessary but too late, because the first input line has already been parsed by that time (thanks, @EdMorton) - either set it in a BEGIN block (BEGIN { FS="," }) or, as in my answer, with command-line option -F (-F ',').
  • if($3=="") {...}
    You only produce output if field $3 is empty - presumably, though, you want to output all lines, so with this approach you'd need an else branch (to print unmodified lines).
  • print $1,$2,"No value"
    The , chars. here are part of the syntax - they simply separate the arguments passed to print. Given separate arguments, print concatenates them with the value of the special OFS variable, whose value is a single space by default; to use , instead, you have to assign it to OFS - again, either in a BEGIN block or via the -v option (-v OFS=',').

You should post some expected output but I THINK what you want is:

awk 'BEGIN{FS=OFS=","} NF{print $1, $2, ($3=="" ? "No value" : $3)}' file

With this file

cat file
[00:00:00.284],501,
[00:00:00.417],5,5294100071980
[00:00:02.463],501,
[00:00:05.169],501,
[00:00:05.529],501,
[00:00:05.730],501,

This awk should do

awk -F, '$3=="" {$3="No value"}1' OFS=, file
[00:00:00.284],501,No value
[00:00:00.417],5,5294100071980
[00:00:02.463],501,No value
[00:00:05.169],501,No value
[00:00:05.529],501,No value
[00:00:05.730],501,No value
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!