0

I have a csv-file that has items like this:

"";"";"";"";"Park2";"Lamella Koivu Natur Loc 5 3-sauvainen 14x188x2266mm. 3,41m2/PKT.";"PKT";"2";"100,0000";"-596,7500";"1";"0";"Lamella Koivu Natur Loc

- Ruotsalainen, laadukas
- 5G Lukkoponttiparketti, taitettava/painettava
- Helppo asentaa
- 3-sauvainen, mattalakattu
- Lauta 14x188x2266mm
- 3,41m2/pkt, 102,3m2/lava

29,33€/M2";""

What I need is the output to be like this:

"";"";"";"";"Park2";"Lamella Koivu Natur Loc 5 3-sauvainen 14x188x2266mm. 3,41m2/PKT.";"PKT";"2";"100,0000";"-596,7500";"1";"0";"Lamella Koivu Natur Loc<br>
<br>
- Ruotsalainen, laadukas<br>
- 5G Lukkoponttiparketti, taitettava/painettava<br>
- Helppo asentaa<br>
- 3-sauvainen, mattalakattu<br>
- Lauta 14x188x2266mm<br>
- 3,41m2/pkt, 102,3m2/lava<br>
<br>
29,33€/M2";""

Or something else that would work, it does not to be exactly like this. I tested adding br manually and it worked. I just want the item descriptions to have line changes in the webstore. Now they do not, line changes are removed in the importing process (import component in joomla cms).

I am creating the csv-file from a .txt file in a shell script.

1
$ csvsql -H -d ';' --query "SELECT a,b,c,d,e,f,g,h,i,j,k,l,REPLACE(m, char(10), '<br>'||char(10)),n FROM data" data.csv

This uses csvkit to treat the CSV file as a database table, and selects all columns (named a through to n for a total of 14 anonymous columns). It replaces the literal newlines (char(10)) in column 13 (m) with <br> followed by newline.

Result:

,,,,Park2,"Lamella Koivu Natur Loc 5 3-sauvainen 14x188x2266mm. 3,41m2/PKT.",PKT,2,1000000,-5967500,1,0,"Lamella Koivu Natur Loc<br>
<br>
- Ruotsalainen, laadukas<br>
- 5G Lukkoponttiparketti, taitettava/painettava<br>
- Helppo asentaa<br>
- 3-sauvainen, mattalakattu<br>
- Lauta 14x188x2266mm<br>
- 3,41m2/pkt, 102,3m2/lava<br>
<br>
29,33€/M2",

If you want ;-delimiters in the output, pass the result over to csvformat:

$ csvsql -H -d ';' --query "SELECT a,b,c,d,e,f,g,h,i,j,k,l,REPLACE(m, char(10), '<br>'||char(10)),n FROM data" data.csv | csvformat -D ';'
;;;;Park2;Lamella Koivu Natur Loc 5 3-sauvainen 14x188x2266mm. 3,41m2/PKT.;PKT;2;1000000;-5967500;1;0;"Lamella Koivu Natur Loc<br>
<br>
- Ruotsalainen, laadukas<br>
- 5G Lukkoponttiparketti, taitettava/painettava<br>
- Helppo asentaa<br>
- 3-sauvainen, mattalakattu<br>
- Lauta 14x188x2266mm<br>
- 3,41m2/pkt, 102,3m2/lava<br>
<br>
29,33€/M2";
  • May want to add -U1 to the csvformat, if the OP wants to force the quoting. – glenn jackman Feb 9 '17 at 18:15
0
perl -0pe 's/("[^"]*")(?=[;\n])/ $1 =~ s!\n!<br>\n!gr /gse' ex.csv
  • perl -0pe ... slurp the input and in the end print the result
  • s/("[^"]*")(?=[;\n])/ action-perl /ge ... substitute all strings by the result of...
  • $1 =~ s!\n!<br>\n!gr replacing the newlines by <br>\n
0

Using gawk, where RS (the record separator) may be a regexp.

BEGIN {
    RS="\"\n"
}
{
    gsub("\n","<br>\n")
    print
}

This assumes that a " at the end of a line is the end of the record, but that pattern could also match a newline at the start of a field. When that happens, no <br> will be inserted there.

Other versions of awk may not support this. From the awk specification:

If RS contains more than one character, the results are unspecified.
0

I like ruby and its CSV module:

ruby -e '
  require "csv"
  options = {:col_sep => ";", :force_quotes => true}
  new_csv = CSV.generate(options) {|csv|
    CSV.foreach(ARGV.shift, options) {|row|
      row[-2].gsub!(/\n/, "<br>\n")
      csv << row
    }
  }
  puts new_csv
' file.csv

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.