0

I have unquoted newline LF in CSV file that I want to load to database, causing error. Can I use sed or other tools readily available in Linux to remove this newline?

Actual line ending for the CSV file is CRLF, as observed using Notepad++ pilcrow.

3
  • replace LF with what?
    – Jasen
    Mar 27, 2021 at 2:17
  • edited the title
    – idazuwaika
    Mar 27, 2021 at 2:21
  • Do you have an example of a few lines of data with this feature? If the data is properly quoted, a CSV parser would be able to read it, and it would be easy to transform or delete embedded newlines.
    – Kusalananda
    Mar 27, 2021 at 7:33

1 Answer 1

3

With awk implementations that support RS values other than single-character ones, such as GNU awk, you could do:

awk 'BEGIN{RS = ORS = "\r\n"}
     {gsub("\n", ""); print}' < in.csv > out.csv

With sed:

CR=$(printf '\r')
sed -e :1 -e '$q' -e "/$CR\$/b" -e 'N;s/\n//;b1' < in.csv > out.csv

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .