用awk替换CSV文件中的列值 [英] Replace column values in a CSV file with awk
本文介绍了用awk替换CSV文件中的列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个文件
error.log
error.log
[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.
-
{{FS=","}...
在您的单个操作内(由于没有前面的模式而对每条输入行都执行了),您为每行设置了变量FS
-这不仅不必要,而且为时已晚,因为第一条输入行已经被解析(感谢@EdMorton)-要么将其设置在BEGIN
块(BEGIN { FS="," }
)中,要么按照我的回答,使用命令行选项( -F ','
). -
if($3=="") {...}
仅当字段$3
为空时才产生输出-但是,大概想输出 all 行,因此使用这种方法,您需要一个else
分支(以打印未修改的行). -
print $1,$2,"No value"
,
字符.这是语法的一部分-它们只是将传递给print
的参数分开.给定单独的参数,print
将它们与特殊的OFS
变量的值连接起来,该变量的值默认为单个 space ;若要使用,
,则必须再次在BEGIN
块中或通过-v
选项(-v OFS=','
)将其分配给OFS
. {{FS=","}...
Inside your single action - which due to not having a preceding pattern is executed for every input line - you set variableFS
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 aBEGIN
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 anelse
branch (to print unmodified lines).print $1,$2,"No value"
The,
chars. here are part of the syntax - they simply separate the arguments passed toprint
. Given separate arguments,print
concatenates them with the value of the specialOFS
variable, whose value is a single space by default; to use,
instead, you have to assign it toOFS
- again, either in aBEGIN
block or via the-v
option (-v OFS=','
).
以上是我建议您解决问题的方式,但是以下是您原始命令的问题:
The above is how I suggest you approach the problem, but here are the problems with your original command:
这篇关于用awk替换CSV文件中的列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文