The easiest way to do this is to do a regex find and replace.
This is done by either going to Search
on the toolbar and then selecting Replace...
, or by using the key combination CTRL + H.
Ensure that you've got Regular expression
selected in the bottom right and ensure that . matches newline
is unchecked (this is important for the regex to work as intended).
NOTE: This regex solution is assuming that each line has a separate and complete table name (as in, no table names are split across multiple lines and each line only contains a single table name).
Put the following into the "Find" box:
^(.*)
Put the following into the "Replace" box:
--\1\nSELECT * FROM \1;
Explanation
The regex string can be broken down as follows:
The ^
is a start of line anchor and makes sure that the regex matches the very start of each line. This ensures that it doesn't accidentally pick up extra lines.
The (.*)
group is used to match any character (.
) any number of times (*
) and then to make those matched characters available to the replace string (()
) using the placeholder format of $x
, where x is the group number (which increases sequentially, with 1
being the first, 2
being the second and so on).
The replace string can be broken down as follows:
--\1
is used to print two literal -
characters, followed by all the characters that were matched in the regex (the (.*)
group), which are inserted in place of \1
.
The \n
is used to add a new line into the replace string (\r\n
may need to be used instead depending on your operating system, but I'm currently going on the assumption that \n
alone will work).
The SELECT * FROM \1;
section is used to print the string as seen, except \1
is replaced with the matched characters as before (which will be the table name).
Thanks for the quick turn around. The solution works but the only problem is table name is being replaced by $1. Any tuning for this? – RaMs_YearnsToLearn – 2014-09-22T10:15:46.520
@RaMs_YearnsToLearn If you've got the regex option selected in the lower left corner of the find and replace panel, I'm not sure why it wouldn't be replacing it as stated. I tried running the above against the data that you provided and I was getting the output that you linked. In theory, it should either be matching both with regex or neither, not only applying regex matching to the
find
field. Is the rest of the output working, except for the table name? – Matt Champion – 2014-09-22T10:22:10.847@RaMs_YearnsToLearn Actually, upon further inspection, it seems that you can use the format
\1
instead of$1
(although I'm unsure why it would work on my machine and not yours). Does making this change in the replace string fix the issue? If so, I'll update the answer with the extra details. – Matt Champion – 2014-09-22T10:27:15.963I replaced $1 with \1 and it worked. Thanks a lot. – RaMs_YearnsToLearn – 2014-09-22T10:30:06.313