如何使用awk在csv文件中将“”fasdfe“”改为“fasdfe”?

时间:2014-09-02 05:41:57

标签: linux awk

我的file.csv有2列,现在看起来像这样:

"""lkBBW4/9BbP/xKxPodjUPA""",7.767066345448011e-05
"""ogiEVE6t+YVtvR1y/56nKA""",5.406898520316803e-05
"""FxfYC7JayTJ+pYdr2qhHsw""",8.7088198609513e-05
...

有没有办法将其转换为:

"lkBBW4/9BbP/xKxPodjUPA",7.767066345448011e-05
"ogiEVE6t+YVtvR1y/56nKA",5.406898520316803e-05
"FxfYC7JayTJ+pYdr2qhHsw",8.7088198609513e-05
...

在ubuntu中使用awk?

更新了问题:如何更改

"lkBBW4/9BbP/xKxPodjUPA",7.767066345448011e-05
"ogiEVE6t+YVtvR1y/56nKA",5.406898520316803e-05
"FxfYC7JayTJ+pYdr2qhHsw",8.7088198609513e-05
...

lkBBW4/9BbP/xKxPodjUPA,7.767066345448011e-05
ogiEVE6t+YVtvR1y/56nKA,5.406898520316803e-05
FxfYC7JayTJ+pYdr2qhHsw,8.7088198609513e-05
...

3 个答案:

答案 0 :(得分:1)

通过awk,

$ awk -v FS='"""' '{print "\""$2"\""$3}' file
"lkBBW4/9BbP/xKxPodjUPA",7.767066345448011e-05
"ogiEVE6t+YVtvR1y/56nKA",5.406898520316803e-05
"FxfYC7JayTJ+pYdr2qhHsw",8.7088198609513e-05

您可以通过sed,

执行此操作
$ sed 's/"\+/"/g' file
"lkBBW4/9BbP/xKxPodjUPA",7.767066345448011e-05
"ogiEVE6t+YVtvR1y/56nKA",5.406898520316803e-05
"FxfYC7JayTJ+pYdr2qhHsw",8.7088198609513e-05

回答更新后的问题:

$ awk -v FS='"' '{print $2$3}' file
lkBBW4/9BbP/xKxPodjUPA,7.767066345448011e-05
ogiEVE6t+YVtvR1y/56nKA,5.406898520316803e-05
FxfYC7JayTJ+pYdr2qhHsw,8.7088198609513e-05

OR

$ awk '{gsub(/"/,"")}1' file
lkBBW4/9BbP/xKxPodjUPA,7.767066345448011e-05
ogiEVE6t+YVtvR1y/56nKA,5.406898520316803e-05
FxfYC7JayTJ+pYdr2qhHsw,8.7088198609513e-05

如果您想在单个命令中执行上述步骤,请尝试以下awk代码。(即删除所有"""

$ cat file
"""lkBBW4/9BbP/xKxPodjUPA""",7.767066345448011e-05
"""ogiEVE6t+YVtvR1y/56nKA""",5.406898520316803e-05
"""FxfYC7JayTJ+pYdr2qhHsw""",8.7088198609513e-05

$ awk '{gsub(/"""/,"")}1' file
lkBBW4/9BbP/xKxPodjUPA,7.767066345448011e-05
ogiEVE6t+YVtvR1y/56nKA,5.406898520316803e-05
FxfYC7JayTJ+pYdr2qhHsw,8.7088198609513e-05

答案 1 :(得分:1)

试试这个:

$ cat f.csv
"""lkBBW4/9BbP/xKxPodjUPA""",7.767066345448011e-05
"""ogiEVE6t+YVtvR1y/56nKA""",5.406898520316803e-05
"""FxfYC7JayTJ+pYdr2qhHsw""",8.7088198609513e-05

$ awk '{gsub(/\042+/,"\042",$1); print}' f.csv
"lkBBW4/9BbP/xKxPodjUPA",7.767066345448011e-05
"ogiEVE6t+YVtvR1y/56nKA",5.406898520316803e-05
"FxfYC7JayTJ+pYdr2qhHsw",8.7088198609513e-05

$ awk '{gsub(/\042+/,"",$1); print}' f.csv
lkBBW4/9BbP/xKxPodjUPA,7.767066345448011e-05
ogiEVE6t+YVtvR1y/56nKA,5.406898520316803e-05
FxfYC7JayTJ+pYdr2qhHsw,8.7088198609513e-05

您必须使用八进制代码来确保所有内容都已正确转义。

答案 2 :(得分:0)

我不认为您需要使用awk,这对我来说就像一个简单的替换,可以使用sed来完成:

sed 's/"""/"/g' file

或者第二个sed 's/"//g' file