The only reason I wanted to use vi commands is because that's what I knew how to do. I did not know there was a LINES keyword that would read in a file line-by-line.
How does the script know what file to read with the LINES keyword?
Here's the problem that I'm finding.
When I run it like you coded, I receive the message 'Syntax error at line 33: `<' is not matched' which refers to the 'done <<' part of the script.
If I take out the '<< fileNAME' after the done, it appears that the file is never created.
Thanks for your help as I do not have experience with a script like this yet.
is just a way to step through a file. With each iteration of the "while" loop, the next line in fileName is stored in someVariable.
So LINE is just what I chose to call the variable. If you take out the part you took out, then the script will hang-up because it doesn't have a filename to read from (at that point, it's actually waiting for the user to provide the input).
However, I screwed up - replace done << someFile with done < someFile.
Thank you for the help, the script works. I put the output file name in the done < inputFile part, I didn't realize that was supposed to be in input file.
Thanks for your help, this knowledge will benefit me greatly.
I believe your requirement was to print the altered line (the "DROP" line) before the original, i.e., to drop a table and then re-create it. I think this will work as well. If your source file is thousands of lines long, it will also be appreciably faster.
awk '/CREATE TABLE/ {
s = $0
k = index(s, "CREATE")
s = substr(s,1,k-1) "DROP" substr(s,k+6)
k = index(s, "(")
s = substr(s,1,k-1) " ;"
print s
}
{ print }' < infile > outfile # print all lines
This should work in all awks. See below for nawk notes.
This assumes that your "CREATE TABLE" statement is completely contained on one line, which I think is implicit in the vi keystrokes you originally posted.
Re: nawk
nawk (or gawk or /usr/xpg4/bin/awk or ...) has facilities that make dealing with substitution and mixed case input a bit easier:
Actually, you might be creating far more work for yourself than is necessary.
If you are trying to create an sql to create a sequence of drop commands for tables that have been created you might consider accessing the RDBMS catalog tables to do this.
In Informix I would do somthing like:
select "drop ", tabname, ";"
from systables
where tabid > 99;
The results can be saved to a file and subsequently used as an sql with a minumum of editing. You may further qualify the tables to drop with additional clauses to the where part, or by unioning a sequence of similar sqls together.
This is for Informix, and the solution will differ for other RDBMS, like Oracle or Sql Server.
Thanks for the info, mbb. I had not thought about that, and would be a good solution for just drop commands.
However, what I'm trying to accomplish is to take the DDL in PROD and re-create those tables in TEST. So I'm doing a db2look command against PROD to get the DDL, and then adding a line before each CREATE command to DROP the table being created.
It could be two steps though - one for dropping and one for creating. I'll have to see which works better. Thanks again.