Read csv file data when data have newline in awk

CSV file can have data with new line . it can be with any column. Also some line can have data without any new line so it should work in all case

Sample input

ID,username,mobile,city,Message,Address,city '11111111',TestUSer,1234567890,test,"Hi how are you? Well: we will connnect

Thanks for your time! With Joy. Test",Address test,City test 11111116,TestUser,1234567891,test,hello msg,Address test1,City test1 '111111167',TestUSer,1234567890,test,"Hi how are you one? Well: we will connnect

Thanks for your time! With Joy. Test",Address test,City test 11111112,TestUser,1234567891,test1,hello msg1,Address test2,City test2 11111113,TestUser,1234567891,test1,hello msg1,Address test2,City test2 11111114,TestUser,1234567891,test1,hello msg1,Address test2,City test2

I am using below command to read top 10 record of csv

awk -v RS='("[^"]*")?\r?\n' 'NF{ORS = gensub(/\r?\n(.)/, "\\\\n\\1", "g", RT);  ++n; print} n==10{exit}' file.csv

Sample output:

ID,username,mobile,city,Message,Address,city
'11111111',TestUSer,1234567890,test,"Hi how are you? Well: we will connnect\nThanks for your time!\nWith Joy.\Test",Address test,City test
11111116,TestUser,1234567891,test,hello msg,Address test1,City test1
'111111167',TestUSer,1234567890,test,"Hi how are you one? Well: we will connnect\nThanks for your time!\nWith Joy.\nTest",Address test,City test
11111112,TestUser,1234567891,test1,hello msg1,Address test2,City test2
11111113,TestUser,1234567891,test1,hello msg1,Address test2,City test2
11111114,TestUser,1234567891,test1,hello msg1,Address test2,City test2


Read more here: https://stackoverflow.com/questions/68485135/read-csv-file-data-when-data-have-newline-in-awk

Content Attribution

This content was originally published by user13000875 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: