如果行不以“结尾"如何删除换行符 [英] How to delete newline if the line doesn't end with "

查看:57
本文介绍了如果行不以“结尾"如何删除换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例数据:

"data","123"
"data2","qwer"
"false","234
And i'm the culprit"
"data5","234567"

输出文本应该是

"data","123"
"data2","qwer"
"false","234And i'm the culprit"
"data5","234567"

本质上,我想修复我的 csv 文件(非常大)

In essence, I want to fix my csv file (which is very large)

我正在使用 sed,因此 sed 中的答案会很有帮助 :)

I'm using sed so an answer in sed would help a lot :)

推荐答案

对于涉及多行的任何问题,sed 始终是错误的选择.只需使用 awk:

sed is always the wrong choice for any problem that involves multiple lines. Just use awk:

$ awk '{printf "%s%s", (prev~/"$/?RS:""), $0; prev=$0} END{print ""}' file
"data","123"
"data2","qwer"
"false","234And i'm the culprit"
"data5","234567"

上面只是检查上一行是否以 " 结尾,如果是,则打印默认的记录分隔符(这是一个换行符 - 您可以用 ORS 或硬编码替换 RS"\n" 如果你愿意)但如果没有,那么它不会打印任何内容.然后它会打印当前记录,之后没有换行符.在所有内容的末尾,它会打印一个换行符.

The above just checks if the previous line ended with a " and if it did then it prints the default Record Separator (which is a newline - you could replace RS with ORS or a hard-coded "\n" if you prefer) but if it didn't then it doesn't print anything. Then it prints the current record without a newline after it. At the end of everything it prints a newline.

这篇关于如果行不以“结尾"如何删除换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆