I usually import data from csv files to MySQL, but my data provider leaves NULL entries as "", so I need to replace "" with "\N". This is easy enough with a script like
for csvfile in *.csv
do
sed -i -e 's/^,/\\N,/' -e 's/,$/,\\N/' -e 's/,,/,\\N,/g' -e 's/,,/,\\N,/g' $csvfile
done
However, I have a csv file with commas, so the import fails. I got the file as "tab delimited" and tried
for txtfile in *.txt
do
sed -i -e 's/^\\t/\\N\\t/' -e 's/\\t$/\\t\\N/' -e 's/\\t\\t/\\t\\N\\t/g' -e 's/\\t\\t/\\t\\N\\t/g' $txtfile
done
But it still fails (as far as I can tell, the script doesn't add any "\N"). When I open the tab-delimited file in Vim and type :set list
it looks like tabs are stored instead as "^I". I tried replacing "\t" with "\^I", but doesn't add the "\N" NULL characters that I need.
Any ideas? Thanks!
Best Answer
You already have an answer for what the ^Is are, but one reason your second
sed
command fails is that you're using the wrong sequence for matching a tab. The sequence that matches a tab is\t
, not\\t
.\\t
matches a\
followed by at
.