Question: I have a csv file like below. Sometimes there is line feed in the middle column. is there any way to replace line feed in unix with space using sed, awk or anything else? -

```
COLA,COLB,COLC,COLD
test,test2,"test3",test4
test5,"test6
test666
test67","test7",test8
test9,test10,test11,test12

```

in above data the second record is actually one record - 

```
test5,"test6
test666
test67","test7",test8

```

Answer: 
Using Miller (<https://github.com/johnkerl/miller>) and running

```sh
mlr --csv put '
  for (k in $*) {
    $[k] = gsub($[k], "\n", " ");
  }
' input.csv >output.csv

```

you will have

```
COLA,COLB,COLC,COLD
test,test2,test3,test4
test5,test6 test666 test67,test7,test8
test9,test10,test11,test12

```
The standard command-line tools, even awk, don't fully understand CSV. You may be able to fashion something that works properly with a more advanced language like python/perl/ruby/GO/etc.

If all you want to do is collapse split lines, the following hack might do:

```
awk -F, 'NF==4{print;next} NF<4{x=(x~/./?x" ":"") $0; if(split(x,a,FS)>=4){print x;x=""}}' input.csv

```

Or for easier reading:

```
BEGIN {
 FS=OFS=","
}

NF==4 {
  print
  next
}

NF<4 {
  x=(x ~ /./ ? x" " : "") $0
  if (split(x,a,FS)>=4) {
    print x
    x=""
  }
}

```

This will print-and-move-on if a line has 4 fields. For any smaller field count, it will begin concatenating lines until it sees at least 4 fields, then will print what it's collected. Note that the return value of the `split()` function should be the item count of the array created by the split.