[英]Replace a value for the nth column in a csv file with sed /awk/any linux tool
I want to replace the 28th column in a csv file from '2020-03-08' to '2020-03-08 00:00' .我想将 csv 文件中的第 28 列从 '2020-03-08' 替换为 '2020-03-08 00:00' 。 I have tried :
gawk -i inplace -F"," '{sub($28,"&00:00"); print}' $filename
我试过:
gawk -i inplace -F"," '{sub($28,"&00:00"); print}' $filename
gawk -i inplace -F"," '{sub($28,"&00:00"); print}' $filename
. gawk -i inplace -F"," '{sub($28,"&00:00"); print}' $filename
。 This misfires because $28 is a date which might even be empty , so it replaces any value in the file not specific 28th column .这会失败,因为 $28 是一个甚至可能为空的日期,因此它会替换文件中不特定于第 28 列的任何值。
Also, 2020-03-08 can be present anywhere in the csv file , it can get replaced instead of the 28th column .此外,2020-03-08 可以出现在 csv 文件中的任何位置,它可以被替换而不是第 28 列。 How can i resolve this ?
我该如何解决这个问题?
Eg of a line in csv file:例如 csv 文件中的一行:
1057481434,e0e529d7-0942-44b1-b26d-794809edb113,2020-03-08 00:20:54+00,2020-03-07 13:50:28+00,fc7557d9-900b-4739-a678-79273dbbcaaf,android,indoor,stationary,100,1774,1774,105,336305e8-abfc-41b6-a07e-50c297c517bc,indoor,f,,,t,,,2020-03-07 13:50:27.539555+00,2020-03-07 13:50:27.539555+00,,31.6838,76.5255,1600,0,2020-03-08,0,,,0,0,0,,"31.6838125,76.52552",America/New_York,ASUS_X00TD,asus,asus,0.8.0,com.pepkit.ssg,83,e172ebc7-a301-4ed0-98a8-d1b852cc2235
You are taking the string in $28
and replacing it anywhere on the line.您以
$28
拿走绳子并将其替换在线路上的任何地方。 Try尝试
{ $28 = $28 " 00:00" }1
or, if you insist on using sub
,或者,如果您坚持使用
sub
,
{ sub(/$/, " 00:00", $28) }1
Notice how the third argument indicates where to replace, instead of on the entire input line.通知第三个参数如何指示,而不是对整个输入线,其中,以取代,。
If you only want to replace when $28
is non-empty, make the action conditional.如果您只想在
$28
非空时进行替换,请将操作设置为有条件的。
$28 { $28 = $28 " 00:00" }1
The final 1
is a common shorthand for最后一个
1
是一个常见的简写
{ print }
Recall that both the condition and the action are optional.回想一下,条件和动作都是可选的。 If the condition is empty, the action is taken unconditionally.
如果条件为空,则无条件执行操作。 If the action is empty, the default action is to print the current input line.
如果动作为空,默认动作是打印当前输入行。 The lone
1
is a condition which is always true.唯一的
1
是一个始终为真的条件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.