Tuesday, 10 September 2013

SQLite import fields with new lines

SQLite import fields with new lines

I have some data that is delimited by the \002 character. This is what I
use to import it:
cat data.txt \
| tr -d '\r' \
| tr '\n' '\003' \
| tr '\t' ' ' \
| tr '\001' '\t' \
| tr '\002' '\n' \
| sed 's%\003%\\n%g' \
> data-formatted.txt
This command erases \r characters, changes \n to \\n, and changes \002 to
new lines \n
Formatting before:
Not Escaped:
1 new
line 2 new
line
2
Escaped:
1\001new\nline\002\001
2\001new\nline\n2\002\001

Formatting after:
Not Escaped:
1 new\\nline
2 new\\nline\\n2
Escaped:
1\tnew\\nline\t\n
2\tnew\\nline\\n2\t\n
The problem is that my data is importing the \n instead of a new line.
Here is my import script:
import.txt:
.separator "\t"
.import data-formatted.txt my_table
And my import command: cat import.txt | sqlite3 my.db
To test the data:
echo 'SELECT * FROM my_table;' | sqlite3 my.db
1|new\nline|
2|new\nline\n2|

How do I import \n as a newline in the data field?

No comments:

Post a Comment