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

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!

2 Answers

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.

1

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

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like