sed编辑csv文件中的特定列和行 [英] sed editing specific column and row in a csv file

查看:381
本文介绍了sed编辑csv文件中的特定列和行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个项目,要求我编辑csv文件的特定列和行.我已经成功找到了正确的行,获取了行号,甚至将特定的列更新为标准输出,但未在.csv文件中.我遇到的问题是将所有内容放在一起,然后更新文件中的值.

I am working on a project that requires me to edit a specific column and row of a csv file. I have been successful at finding the correct row, getting the row number and even updating the specific column as standard output but not in the .csv file. The problem I have is putting it all together and getting sed to update the values in the file.

以下是csv中值的示例:

Here is an example of the values in the csv:

9847,3132,132,0,168,0,410,73,4400,DC,4300,6,248,Yes,Yes,Yes,Yes,Yes,No,No,No,No,No,LSI,Service,Yes,No,No,pw,Yes,SA
9848,132,3132,0,168,0,410,73,4400,DC,4300,6,248,Yes,Yes,Yes,Yes,Yes,No,No,No,No,No,LSI,Service,Yes,No,No,pw,Yes,SA
9849,3132,132,0,168,0,410,73,4400,DC,4300,6,248,Yes,Yes,Yes,Yes,Yes,No,No,No,No,No,LSI,Service,Yes,No,Yes,pw,Yes,SA
9850,3132,132,0,168,0,410,73,4400,DC,4300,6,248,Yes,Yes,Yes,Yes,Yes,No,No,No,No,No,LSI,Service,Yes,No,Yes,pw,Yes,SA
9851,3132,132,0,168,0,410,73,4400,DC,4300,6,248,Yes,Yes,Yes,Yes,Yes,No,No,No,No,No,LSI,Service,Yes,No,Yes,pw,Yes,SA
9852,3132,132,0,168,0,410,73,4400,DC,4300,6,248,Yes,Yes,Yes,Yes,Yes,No,No,No,No,No,LSI,Service,Yes,No,Yes,pw,Yes,SA
9853,3132,132,0,168,0,410,73,4400,DC,4300,6,248,Yes,Yes,Yes,Yes,Yes,No,No,No,No,No,LSI,Service,Yes,No,Yes,pw,Yes,SA
9854,3132,132,0,168,0,410,73,4400,DC,4300,6,248,Yes,Yes,Yes,Yes,Yes,No,No,No,No,No,LSI,Service,Yes,No,Yes,pw,Yes,SA
9855,3132,132,0,168,0,410,73,4400,DC,4300,6,248,Yes,Yes,Yes,Yes,Yes,No,No,No,No,No,LSI,Service,Yes,No,Yes,pw,Yes,SA
9856,3132,132,0,168,0,410,73,4400,DC,4300,6,248,Yes,Yes,Yes,Yes,Yes,No,No,No,No,No,LSI,Service,Yes,No,No,pw,Yes,SA
9857,3132,132,0,168,0,410,73,4400,DC,4300,6,248,Yes,Yes,Yes,Yes,Yes,No,No,No,No,No,LSI,Service,Yes,No,Yes,pw,Yes,SA

在此示例中,我要将项目号9856的第28列修改为是".到目前为止,我成功的命令包括:​​

In this example I want to modify the 28th column for item number 9856 to Yes. My successful commands so far include:

此命令为我希望行显示的方式提供了正确的返回.

This command gives me a correct return for how I want the line to appear.

grep 9856 ./file.csv | awk -F, '$28="Yes"' 

从这里开始,我假设我需要将结果传递给sed命令,该命令将用更新后的行替换现有行,但是我的所有尝试均未成功!

From here I assume I need to pipe the results to a sed command that will replace the existing line with the updated line but all of my attempts have been unsuccessful!

也请您出于我的学习目的详细说明您的答案.我还将在这个项目中编辑许多其他列值.

Also if you could please elaborate on your answers for my learning purposes. I will also be editing many other column values in this project.

推荐答案

使用GNU sed:

sed '/^9856,/s/[^,]*/Yes/28' file.csv

说明

  • /^9856,/:如果行以^9856,
  • 开头
  • s/[^,]*/Yes/28:将包含零个或多个(*)非逗号字符([^,]*)的字符串的第28次出现替换为Yes
  • /^9856,/: if line starts with ^9856,
  • s/[^,]*/Yes/28: replace 28th occurrence of string containing zero or more(*) non-comma character([^,]* with Yes

要在适当位置编辑文件,请添加-i标志:

To edit the file in place, add the -i flag:

sed -i '/^9856,/s/[^,]*/Yes/28' file.csv

这篇关于sed编辑csv文件中的特定列和行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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