如何从.CSV文件的两列中删除撇号(')?

时间:2022-01-10 22:29:43

I have a .CSV file with 7 fields, and the 3rd and 4th columns of the file has a number starting with an apostrophe ( ' ). Please see the example below.

我有一个带有7个字段的. csv文件,文件的第3和第4列有一个以撇号(')开头的数字。请参见下面的示例。

col0,col1,col2,col3,col4,col5,col6,
1value0,1value1,'8972991766941,'8972991766941,1value4,1value5,1value6,
2value0,2value1,'8912988876583,'8912988876583,2value4,2value5,2value6,
3value,3value1,'8912981226981,'8912981226981,3value4,3value5,3value6,
2value0,4value1,'8912971783681,'8912971783681,4value4,4value5,4value6,

How do I get rid of the apostrophes in the 3rd and 4th columns only using either sed or awk?

如何只使用sed或awk去掉第3和第4列中的撇号?

3 个解决方案

#1


4  

You can use awk:

您可以使用awk:

awk -F, '{for (i=3;i<=4;i++) sub("'\''", "", $i)}1' OFS=, file

#2


2  

If ' is not needed at all, remove it everywhere.

如果“根本不需要”,则在任何地方删除它。

awk '{gsub("'\''","")}1' file

#3


-3  

Press CTRL+H on Notepad, replace "'" with "" (Nothing)

在记事本上按CTRL+H,将“'”替换为“”(无)

#1


4  

You can use awk:

您可以使用awk:

awk -F, '{for (i=3;i<=4;i++) sub("'\''", "", $i)}1' OFS=, file

#2


2  

If ' is not needed at all, remove it everywhere.

如果“根本不需要”,则在任何地方删除它。

awk '{gsub("'\''","")}1' file

#3


-3  

Press CTRL+H on Notepad, replace "'" with "" (Nothing)

在记事本上按CTRL+H,将“'”替换为“”(无)