What is the ^I character and how do I find it with sed?

5

1

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!

Richard Herron

Posted 2011-06-23T19:54:01.093

Reputation: 1 027

Answers

2

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.

garyjohn

Posted 2011-06-23T19:54:01.093

Reputation: 29 085

Makes sense. I am confusing wanting an explicit "\N", which I add with "\N", with looking for the tab. Thanks! – Richard Herron – 2011-06-23T21:22:35.930

6

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

sgmoore

Posted 2011-06-23T19:54:01.093

Reputation: 5 961

Do you know how I can make my sed work? I tried replacing "\t" with "^I" but it didn't work? – Richard Herron – 2011-06-23T21:18:04.110