replace word in line only if line number start with + csv file

时间:2023-01-14 15:30:29

I use the following sed command in order to replace string in CSV line

我使用以下sed命令来替换CSV行中的字符串

( the condition to replace the string is to match the number in the beginning of the CSV file )

(替换字符串的条件是匹配CSV文件开头的数字)

SERIAL_NUM=1


sed "/$SERIAL_NUM/ s//OK/g" file.csv

the problem is that I want to match only the number that start in the beginning of the line ,

问题是我想只匹配从行开头开始的数字,

but sed match other lines that have this number

但是sed匹配具有此数字的其他行

example:

例:

in this example I want to replace the word - STATUS to OK but only in line that start with 1 ( before the "," separator )

在这个例子中,我想将单词 - STATUS替换为OK但仅在以1开头的行中(在“,”分隔符之前)

so I do this

所以我这样做

 SERIAL_NUM=1


 more file.csv


 1,14556,43634,266,242,def,45,STATUS
 2,4345,1,43,57,24,657,SD,STATUS
 3,1,WQ,435,676,90,3,44f,STATUS


 sed -i "/$SERIAL_NUM/ s/STATUS/OK/g" file.csv


 more file.csv

 1,14556,43634,266,242,def,45,OK
 2,4345,1,43,57,24,657,SD,OK
 3,1,WQ,435,676,90,3,44f,OK

but sed also replace the STATUS to OK also in line 2 and line 3 ( because those lines have the number 1 )

但sed也将第2行和第3行的状态替换为OK(因为这些行的数字为1)

please advice how to change the sed syntax in order to match only the number that start the line before the "," separator

请建议如何更改sed语法,以便仅匹配在“,”分隔符之前开始行的数字

remark - solution can be also with perl line liner or awk ,

备注 - 解决方案也可以使用perl line liner或awk,

2 个解决方案

#1


3  

You can use anchor ^ to make sure $SERIAL_NUM only matches at start and use , after that to make sure there is a comma followed by this number:

你可以使用anchor ^来确保$ SERIAL_NUM仅在开始和使用时匹配,之后确保有一个逗号后跟这个数字:

sed "/^$SERIAL_NUM,/s/STATUS/OK/g" file.csv

#2


0  

Since this answer was ranked fifth in the * perl report but had no perl content, I thought it would be useful to add the following - instead of removing the perl tag :-)

由于这个答案在* perl报告中排名第五,但没有perl内容,我认为添加以下内容会很有用 - 而不是删除perl标记:-)

#!/usr/bin/env perl
use strict;
use warnings; 

while(<DATA>){
  s/STATUS/OK/g if /^1\,/;
  print ;
}

__DATA__
1,14556,43634,266,242,def,45,STATUS
2,4345,1,43,57,24,657,SD,STATUS
3,1,WQ,435,676,90,3,44f,STATUS

or as one line:

或作为一行:

perl -ne 's/STATUS/OK/g if /^1\,/;' file.csv

#1


3  

You can use anchor ^ to make sure $SERIAL_NUM only matches at start and use , after that to make sure there is a comma followed by this number:

你可以使用anchor ^来确保$ SERIAL_NUM仅在开始和使用时匹配,之后确保有一个逗号后跟这个数字:

sed "/^$SERIAL_NUM,/s/STATUS/OK/g" file.csv

#2


0  

Since this answer was ranked fifth in the * perl report but had no perl content, I thought it would be useful to add the following - instead of removing the perl tag :-)

由于这个答案在* perl报告中排名第五,但没有perl内容,我认为添加以下内容会很有用 - 而不是删除perl标记:-)

#!/usr/bin/env perl
use strict;
use warnings; 

while(<DATA>){
  s/STATUS/OK/g if /^1\,/;
  print ;
}

__DATA__
1,14556,43634,266,242,def,45,STATUS
2,4345,1,43,57,24,657,SD,STATUS
3,1,WQ,435,676,90,3,44f,STATUS

or as one line:

或作为一行:

perl -ne 's/STATUS/OK/g if /^1\,/;' file.csv