linux - bash 粘贴不保留换行符

标签 linux bash paste

我的 bash 脚本是从 file1.txt 粘贴的,其中逐行输入 CSV 格式。例如:

File1.txt 包含文本:

John
21-2-2015
some city
"108 Brent Street
Ridgewoods
sometown
somecountry"

CSV 上传的转换输出应如下所示:

John,21-2-2015,some city,"108 Brent Street
Ridgewoods
sometown
somecountry"

但是我的代码将其转换为:

John,21-2-2015,some city,"108 Brent Street Ridgewoods sometown somecountry"

我想保留换行符。

我的代码:

paste -sd, file1.txt > file2.csv

最佳答案

您可以尝试以下 Perl 命令。

$ perl -0777pe 's/(?s)"[^"]*"(*SKIP)(*F)|\n(?!$)/,/g' file
John,21-2-2015,some city,"108 Brent Street
Ridgewoods
sometown
somecountry"

添加-i 参数以保存对该文件所做的更改。

关于linux - bash 粘贴不保留换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28698771/

相关文章:

Python 脚本调用 Make 和其他实用程序

linux - 获取模式第一次出现和第 n 次出现之间的子字符串

linux - Grep .html 语法

bash - bash 脚本中 stderr 的临时重定向

Vim 粘贴行为

linux - insmod : ERROR: could not insert module : No child processes

c++ - 从命令行开始?

python - 两个列表,如果找到一个共同的匹配打印另一个列表元素

javascript - 将粘贴事件 Hook 到隐藏的文本区域

javascript - 将图像粘贴到网页中