简体   繁体   English

用 sed /awk/any linux 工具替换 csv 文件中第 n 列的值

[英]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.

相关问题 在CSV中选择与GNU Linux中的模式文件中的任何模式都不匹配的行(AWK / SED / GREP) - Select rows in a CSV not matching any pattern in pattern file in GNU Linux (AWK/SED/GREP) awk或sed更改文件中的列值 - awk or sed to change column value in a file 如何使用 AWK 或 SED 替换 CSV 文件中的多个列? - How to replace multiple columns in CSV file using AWK or SED? 使用bash,sed或awk拆分CSV文件并排除输出中的列 - Splitting CSV file and excluding column in output using bash, sed or awk 使用Sed / awk / grep或任何其他工具在Linux环境中提取子字符串 - Extract substring in Linux environemnt using Sed/awk/grep or any other tool 如果父键在 json 文件中匹配,SED/AWK 如何替换值 - SED/AWK how to replace a value if a parent key matches in json file 如何使用awk工具对齐csv文件中的列 - how to align column from csv file using awk tool 如何使用sed或任何其他Unix工具替换具有特定键值的XML文件中的所有值字段? - How can I replace all value fields in an XML file having particular key value using sed or any other Unix tool? 如何在Linux bash中有条件提取csv文件的第n列? - How to extract the nth column of csv file with condition in Linux bash? 如何在Linux输出中替换列数据-可能正在使用awk sed等 - How to replace a column data in linux output - may be using awk sed etc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM