将行转换为 CSV 文件的命令

将行转换为 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",

我想将其转换为 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但遇到了麻烦,因为它将所有输出打印到一行,并且似乎用双换行符替换换行符。任何帮助表示赞赏。

答案1

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
}

答案2

<file sed '
   :start
   s/\n$//
   t
   s/\n //
   N
   b start
  ' | sed 's/,$//'

第一个sed循环 ( :start, b start) 并将行追加到其模式空间 ( N),直到找到并删除最后的换行符 ( s/\n$//)。这表示读取了空行,然后工具退出循环 ( t)。在每次迭代中,任何幸存的换行符(和连续的空格)都会被删除以连接行(s/\n //)。

第二个sed删除尾随逗号。

相关内容