Pablo Boswell
Pablo Boswell

Reputation: 845

Using vim on Windows to parse a .sql file

My Generate Scripts SQL file including my CREATE commands for various views and tables is too large for a regular notepad editor. I am using vim. However, every character has a ^@ between it. So CREATE TABLE looks like this:

^@C^@R^@E^@A^@T^@E^@ ^@T^@A^@B^@L^@E

Is this normal? Basically, there are only a few CREATE scripts I really want in this file. Particularly, I am looking for any line that has [dbo].[v_agent in it. I understand grep commands, but I am sitting waiting for the command to run and want to know if I am wasting my time or not.

Upvotes: 0

Views: 806

Answers (1)

Anthony Geoghegan
Anthony Geoghegan

Reputation: 12003

Modern MS Windows programs and utilities (such as SQL Server) create text files using the UTF-16 LE (little end) character encoding.

By default, Unix utilities such as grep don’t interpret such files as being text files and instead treat them as binary files (streams of bytes).


If you’re running a Unix emulator (e.g., Cygwin), the iconv tool could be used to convert this to a different encoding such as UTF-8, e.g.

iconv -f utf16le -t utf8 in.sql > out.sql

Since you’ve already stated that you have the file open in Vim, you can re-open it using this Vim command:

:set encoding=utf8
:e ++encoding=utf16le

The first command is used to make sure that Unicode characters can be represented correctly when the file is loaded into a Vim buffer. It should not be necessary as the default encoding is usually set to UTF-8.

Vim doesn’t automatically recognise the file as being encoded in UTF-16LE so the second command re-opens the file for editing; you have to tell it which character encoding that should be used to interpret the stream of bytes that make up the file. For more info, see http://vim.wikia.com/wiki/Reloading_a_file_using_a_different_encoding

Now that the file is loaded correctly into a Vim buffer, you could set the fileencoding option to any character encoding that you would like to save the file with.

Upvotes: 3

Related Questions