Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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!

share|improve this question
up vote 1 down vote accepted

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 a t.

share|improve this answer
    
Makes sense. I am confusing wanting an explicit "\N", which I add with "\\N", with looking for the tab. Thanks! – Richard Herron Jun 23 '11 at 21:22

^ is normally shorthand for Ctrl and Ctrl-I is the same as Tab

share|improve this answer
    
Do you know how I can make my sed work? I tried replacing "\\t" with "\^I" but it didn't work? – Richard Herron Jun 23 '11 at 21:18

You must log in to answer this question.

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