3

I have a file of the format, with a leading space before each line:

 "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",

I would like to convert it to a CSV file with format:

 "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"

I'm trying to use tr but am having trouble since it either prints all output to one line and seems to replace newlines with a double newline. Any help is appreciated.

2 Answers2

5

An awk solution is

awk '{if(NF){gsub(/^ |,$/,""); printf c $0; c=","}else{printf "\n"; c=""}};END{printf "\n"}'

expanded with comments:

{
    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
}
Dabombber
  • 302
1
<file sed '
   :start
   s/\n$//
   t
   s/\n //
   N
   b start
  ' | sed 's/,$//'

The first sed loops (:start, b start) and appends lines to its pattern space (N) until a newline at the very end is found and deleted (s/\n$//). This indicates an empty line was read, the tool exits the loop then (t). At each iteration any surviving newline (and a consecutive space) is removed anyway to concatenate lines (s/\n //).

The second sed removes trailing commas.