0

I'm trying to convert a MSSQL .sql file to MYSQL syntax file. I have managed to change some of the syntax using basic find/replace, but I also need to change the ordering of words in the line. Basically the statements are like this :

ALTER TABLE ADD INDEX index_name ON table_name(column_name)

and I want to correct the statements to this format(MYSQL syntax) :

ALTER TABLE table_name ADD INDEX index_name(column_name)

There are like 70 statements of this sort in the file.Basically, table_name has to be shifted from between ON and (, and put in between TABLE and ADD. Is it possible to do this using sed/awk/grep?

  • Maybe this helps: http://stackoverflow.com/questions/8019144/how-to-migrate-mssql-database-to-mysql ? – hek2mgl Jun 26 '14 at 12:22
  • No I have tried migrating the files but there were too many errors and I feel changing the syntax is simpler. –  Jun 26 '14 at 12:27

2 Answers2

3

The following works in sed for your example input:

sed 's/\(ADD INDEX\) \(.*\) ON \(.*\)(/\3 \1 \2(/'
choroba
  • 231,213
  • 25
  • 204
  • 289
  • Thanks it worked like a charm :). I can't vote you up for now though. –  Jun 26 '14 at 12:33
0

You could use this GNU sed command also,

sed -r 's/^(\w+\s\w+)(\s\w+\s\w+) (\w+) \w+ (\w+)(.*)$/\1 \4\2 \3\5/g' file

Example:

$ echo 'ALTER TABLE ADD INDEX index_name ON table_name(column_name)' | sed -r 's/^(\w+\s\w+)(\s\w+\s\w+) (\w+) \w+ (\w+)(.*)$/\1 \4\2 \3\5/'
ALTER TABLE table_name ADD INDEX index_name(column_name)
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274