用 awk 替换 CSV 文件中的列值 [英] Replace column values in a CSV file with awk

查看:53
本文介绍了用 awk 替换 CSV 文件中的列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个文件

错误日志

[00:00:00.284],501,

[00:00:00.284],501,

[00:00:00.417],5,5294100071980

[00:00:00.417],5,5294100071980

[00:00:02.463],501,

[00:00:02.463],501,

[00:00:05.169],501,

[00:00:05.169],501,

[00:00:05.529],501,

[00:00:05.529],501,

[00:00:05.730],501,

[00:00:05.730],501,

所以,如果字段 $3 为空,我想打印无价值"

so, if the field $3 its empty i want to print "No value"

我正在尝试这段代码

awk '{{FS=","} if($3=="") {print $1,$2,"No value"}}'

但它会打印出来

>[00:00:00.284] 501 No value
>[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
>[00:00:07.193] 501 No value
>[00:00:09.899] 501 No value
>[00:00:31.312] 501 No value

推荐答案

awk -F ',' -v OFS=',' '$1 { if ($3=="") $3="No value"; print}' in.txt

  • 通过 -F 选项传递字段分隔符.
  • 变量OFS,输出字段分隔符,设置为,,这样输出字段也用,分隔.李>
  • 模式 $1 确保只处理非空行(即,仅当第一个字段非空时才执行关联的操作) - 如果您的输入文件没有空行,您可以删除此模式.
  • 如果第三个字段为空,则分配字符串无值"
  • 最后,输出行(带有可能修改的第三个字段).
    • 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=","}...在您的单个操作中 - 由于没有为每个输入行执行前面的模式 - 您为每一行设置变量 FS - 这不仅是不必要的,而且 为时已晚,因为那时第一行已经被解析了(感谢@EdMorton)——或者将它设置在 BEGIN 块中(BEGIN { FS="," }) 或者,如我的回答,使用命令行选项 -F (-F ',').
      • if($3=="") {...}
        只有当字段 $3 为空时,您才会产生输出 - 不过,可能您想输出 all 行,因此使用这种方法,您需要一个 else 分支(打印未修改的行).
      • 打印 $1,$2,"No value"
        , 字符.这是语法的一部分——它们只是将传递给 print 的参数分开.给定单独的参数,print 将它们与特殊的 OFS 变量的值连接起来,默认情况下其值为单个 space;要使用 , 代替,您必须将其分配给 OFS - 再次,在 BEGIN 块中或通过 -v 选项(-v OFS=',').
      • {{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=',').

      这篇关于用 awk 替换 CSV 文件中的列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆