我有一个格式的文件,每一行前面都有一个前导空格:
"Western Overseas",
"Western Overseas",
"^",
"--",
"^",
"--",
"--",
null,
24995,
9977,
"CR",
"Western Refrigeration Private Limited",
"Western Refrigeration Private Limited",
"[ICRA]A",
"--",
"[ICRA]A1",
"--",
"Stable",
null,
14951,
2346,
"CR",我想把它转换成一个格式为CSV的文件:
"Western Overseas","Western Overseas","^","--","^","--","--",null,24995,9977,"CR"
"Western Refrigeration Private Limited","Western Refrigeration Private Limited","[ICRA]A","--","[ICRA]A1","--","Stable",null,14951,2346,"CR"我正在尝试使用tr,但遇到了问题,因为它要么将所有输出打印到一行中,要么用双换行符替换换行符。任何帮助都是非常感谢的。
发布于 2019-07-16 10:53:48
awk解决方案是
awk '{if(NF){gsub(/^ |,$/,""); printf c $0; c=","}else{printf "\n"; c=""}};END{printf "\n"}'经评论后扩大:
{
if(NF) { # if the line isn't empty
gsub(/^ |,$/,""); # remove the first space and last comma
printf c $0; # print the line (without a newline)
c="," # set c to add a comma for the next field
} else {
printf "\n"; # empty line, output a newline
c="" # don't print a comma for the next entry
}
};
END {
printf "\n" # finish off with a newline
}发布于 2019-07-16 08:02:20
第一个sed循环(:start、b start)并将行附加到其模式空间(N),直到找到并删除最末端的换行符(s/\n$//)为止。这表示读取了空行,该工具退出循环,然后(t)。在每次迭代时,任何幸存的换行符(和一个连续的空格)都会被删除,以连接行(s/\n //)。
第二个sed移除后面的逗号。
https://unix.stackexchange.com/questions/530382
复制相似问题