本文介绍了在CSV的Column中搜索模式,并使用sed命令替换同一行中的另一个模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在CSV文件的第二列中检查一个模式(仅当模式开头时),如果该模式存在,则替换同一行中的其他内容.

I want to check for a pattern (only if the pattern starts with) in second column in a CSV file and if that pattern exists then replace something else in same line.

我写了下面的sed命令,用于在csv之后将I更改为N(如果第二列中存在模式676).但是,由于存在676,因此也会在第7列和第9列中检查676.理想情况下,如果前缀676存在,我只希望检查第二行.我要检查的是第二列中的676前缀(模式不在第二个值Ex-46769777的中间或结尾),然后对I进行更改.

I wrote the following sed command for following csv to change the I to N if the pattern 676 exists in second column. But it checks 676 in the 7th and 9th column also since the ,676 exists. Ideally, I want only the second line to be checked for if the prefix 676 exists. All I want is to check 676 prefixed in second column (pattern not in the middle or end of the second value Ex- 46769777) and then do the change on ,I, to ,N,.

sed -i  '/,676/ {; s/,I,/,N,/;}' temp.csc

6768880,55999777,S,I,TTTT,I,67677,yy
6768880,676999777,S,I,TTTT,I,67677,yy 
6768880,46769777,S,I,TTTT,I,67677,yy  

需要预期的结果

6768880,55999777,S,I,TTTT,I,67677,yy
6768880,676999777,S,N,TTTT,N,67677,yy
6768880,40999777,S,I,TTTT,I,67677,yy  

推荐答案

这要求在进行任何更改之前, 676 出现在第二列的开头:

This requires that 676 appear at the beginning of the second column before any changes are made:

$ sed   '/^[^,]*,676/ s/,I,/,N,/g' file
6768880,55999777,S,I,TTTT,I,67677,yy
6768880,676999777,S,N,TTTT,N,67677,yy 
6768880,46769777,S,I,TTTT,I,67677,yy  

注意:

  • 正则表达式/^ [^,] *,676/要求 676 在该行上首次出现逗号后出现.更详细地:

  • The regex /^[^,]*,676/ requires that 676 appear after the first appearance of a comma on the line. In more detail:

  • ^ 匹配行的开头

[^,] * 匹配第一列

,676 与第一个逗号匹配,后跟 676

,676 matches the first comma followed by 676

在所需的输出中,每次出现在行中时,,I,都会被替换为,N,.为此,将 g (表示全局)添加到了替代命令中.

In your desired output, ,I, was replaced with ,N, every time it appeared on the line. To accomplish this, g (meaning global) was added to the substitute command.

这篇关于在CSV的Column中搜索模式,并使用sed命令替换同一行中的另一个模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 13:10