在双引号内删除回车

时间:2016-07-05 18:51:28

标签: bash shell awk sed

我有一个简单的文本文件,我正在尝试使用shell脚本进行bash,但是我一直遇到一些有问题的行。

有一定数量的字段,其中一个字段是自由格式输入。

自由表单字段在双引号内有回车符,我想删除。

格式与此类似:

final ObjectMapper mapper = new ObjectMapper();
mapper.disable(MapperFeature.USE_ANNOTATIONS);
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);

我尝试了以下三种解决方案但没有运气:

  1. "0001","Barker","Bob","Free Form Text Here" "0002","Barker","Jane", "Free Form Text Here" //删除所有回车
  2. tr '\r\n' '' //无效
  3. sed –e 's/\".*\n"//g' //只返回逗号。
  4. 任何想法我可能做错了什么?我可以使用awk -v RS='"[^"]*"' -v ORS= '{gsub(/\n/, " ", RT); print $0 RT}'sed作为解决方案。

1 个答案:

答案 0 :(得分:2)

使用perl

更容易
perl -pe 's/(,\h*"[^\n"]*)\n/$1 /g' file

"0001","Barker","Bob","Free Form Text Here"
"0002","Barker","Jane", "Free Form Text Here"

保存更改内联使用:

perl -i -pe 's/(,\h*"[^\n"]*)\n/$1 /g' file