如果该行不以 " 结尾且尾随空格,如何删除换行符
How to delete newline if the line doesn't end with " with trailing white-spaces
but with another parameter of having trailing white-spaces as ponted out by @Jubobs
示例数据:
"data","123" <-spaces
"data2","qwer" <-space
"false","234 <-spaces
And i'm the culprit" <-- spaces at the start of line and end of line
"data5","234567"
输出文本应该是
"data","123"
"data2","qwer"
"false","234 And i'm the culprit"
"data5","234567"
本质上,我想修复我的 csv 文件(非常大)
我正在使用 sed,所以 sed 中的答案会有很大帮助:)
编辑:为示例文本添加了空格
你可以试试
awk '/[a-zA-Z0-9][^"]*$/{ORS=""} /[a-zA-Z0-9]"[^"]*$/{ORS="\n"} 1 '
测试
$ awk '/[a-zA-Z0-9][^"]*$/{ORS=""} /[a-zA-Z0-9]"[^"]*$/{ORS="\n"} 1 ' input
"data","123"
"data2","qwer"
"false","234And i'm the culprit"
"data5","234567"
它有什么作用?
[a-zA-Z0-9][^"]*$
匹配末尾没有 "
的所有行。
{ORS=""}
设置输出记录分隔符为 ""
[a-zA-Z0-9]"[^"]*$
匹配所有以 "
结尾的行
{ORS="\n"}
设置字段记录分隔符为 \n
我在您的示例输入的末尾添加了一行,其中包含一个以白色开头的字段 space 因为测试它是否适用于您获得的任何建议解决方案很重要:
$ cat file
"data","123"
"data2","qwer"
"false","234
And i'm the culprit"
"data5","234567"
"stuff","
foo"
所以你可以看到换行符和白色 space:
$ sed 's/$/$/' file
"data","123" $
"data2","qwer" $
"false","234 $
And i'm the culprit"$
"data5","234567"$
"stuff"," $
foo"$
如果您只想删除换行符但保留尾随的白色 space 那么您只需要这个 awk 命令(仅通过管道传输到 sed 以显示换行符)
$ awk '{q+=gsub(/"/,"&"); printf "%s%s",[=12=],(q%2?"":RS)}' file | sed 's/$/$/'
"data","123" $
"data2","qwer" $
"false","234 And i'm the culprit"$
"data5","234567"$
"stuff"," foo"$
如果你想删除尾随的白色 space 当它也在字段中时:
$ awk '{q+=gsub(/"/,"&"); if (q%2) sub(/[[:blank:]]+$/,""); printf "%s%s",[=13=],(q%2?"":RS)}' file | sed 's/$/$/'
"data","123" $
"data2","qwer" $
"false","234And i'm the culprit"$
"data5","234567"$
"stuff","foo"$
在上面的所有情况下,sed
命令只是在行尾添加一个 $
以使尾随的白色 space 在这个例子中可见,awk命令就是你所需要的。
它所做的只是计算您到目前为止看到了多少 "
(q+=gsub(/"/,"&")
)。如果它是一个奇数(q%2
是 1
)那么你在一个字段的中间所以不要在行尾打印换行符,否则只打印通常的 Record Separator 这是一个换行符。
这可能对你有用 (GNU sed):
sed -r ':a;s/^(".*",".*").*//;t;N;s/\n//;ta' file
如果该行包含两个用逗号分隔的双引号字段,请删除最后一个双引号后面的所有内容,您就完成了。否则附加下一行并删除其换行符并重试。
示例数据:
"data","123" <-spaces
"data2","qwer" <-space
"false","234 <-spaces
And i'm the culprit" <-- spaces at the start of line and end of line
"data5","234567"
输出文本应该是
"data","123"
"data2","qwer"
"false","234 And i'm the culprit"
"data5","234567"
本质上,我想修复我的 csv 文件(非常大)
我正在使用 sed,所以 sed 中的答案会有很大帮助:)
编辑:为示例文本添加了空格
你可以试试
awk '/[a-zA-Z0-9][^"]*$/{ORS=""} /[a-zA-Z0-9]"[^"]*$/{ORS="\n"} 1 '
测试
$ awk '/[a-zA-Z0-9][^"]*$/{ORS=""} /[a-zA-Z0-9]"[^"]*$/{ORS="\n"} 1 ' input
"data","123"
"data2","qwer"
"false","234And i'm the culprit"
"data5","234567"
它有什么作用?
[a-zA-Z0-9][^"]*$
匹配末尾没有"
的所有行。{ORS=""}
设置输出记录分隔符为""
结尾的行[a-zA-Z0-9]"[^"]*$
匹配所有以"
{ORS="\n"}
设置字段记录分隔符为\n
我在您的示例输入的末尾添加了一行,其中包含一个以白色开头的字段 space 因为测试它是否适用于您获得的任何建议解决方案很重要:
$ cat file
"data","123"
"data2","qwer"
"false","234
And i'm the culprit"
"data5","234567"
"stuff","
foo"
所以你可以看到换行符和白色 space:
$ sed 's/$/$/' file
"data","123" $
"data2","qwer" $
"false","234 $
And i'm the culprit"$
"data5","234567"$
"stuff"," $
foo"$
如果您只想删除换行符但保留尾随的白色 space 那么您只需要这个 awk 命令(仅通过管道传输到 sed 以显示换行符)
$ awk '{q+=gsub(/"/,"&"); printf "%s%s",[=12=],(q%2?"":RS)}' file | sed 's/$/$/'
"data","123" $
"data2","qwer" $
"false","234 And i'm the culprit"$
"data5","234567"$
"stuff"," foo"$
如果你想删除尾随的白色 space 当它也在字段中时:
$ awk '{q+=gsub(/"/,"&"); if (q%2) sub(/[[:blank:]]+$/,""); printf "%s%s",[=13=],(q%2?"":RS)}' file | sed 's/$/$/'
"data","123" $
"data2","qwer" $
"false","234And i'm the culprit"$
"data5","234567"$
"stuff","foo"$
在上面的所有情况下,sed
命令只是在行尾添加一个 $
以使尾随的白色 space 在这个例子中可见,awk命令就是你所需要的。
它所做的只是计算您到目前为止看到了多少 "
(q+=gsub(/"/,"&")
)。如果它是一个奇数(q%2
是 1
)那么你在一个字段的中间所以不要在行尾打印换行符,否则只打印通常的 Record Separator 这是一个换行符。
这可能对你有用 (GNU sed):
sed -r ':a;s/^(".*",".*").*//;t;N;s/\n//;ta' file
如果该行包含两个用逗号分隔的双引号字段,请删除最后一个双引号后面的所有内容,您就完成了。否则附加下一行并删除其换行符并重试。