vi Commands in a Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting vi Commands in a Script
# 1  
Old 07-03-2003
vi Commands in a Script

Perhaps there is a better way to do this, but right now this is all I can think of. If there is a better way to do this, all suggestions are welcome.

I would like to take a file and perform the following actions on it.

1. Search for CREATE TABLE
2. Copy that line and paste it one line above
3. Change the CREATE to a DROP
4. Change the '(' at the end to a ';'
5. Run until the EOF is reached, then save

I have a script written that goes as follows:

vi gl.ddl << EOF
/CREATE TABLE
yy
k
p
cw DROP
$
cw ;
n
n
ZZ
EOF

It currently does not work at all, and throws the following message when I try to execute it.

ex: 0602-134 An error occurred while reading input.

I initially thought it was a file permission problem, so set the perms to 777, but still receive the same message.

Thanks in advance for any insights.

Schmitty
# 2  
Old 07-03-2003
This'll work for you, unless you really wanted to use vi commands for some reason...
Code:
while read LINE
do
 echo $LINE >> resultFile
 echo $LINE | grep -q "CREATE TABLE"
 if [ $? -eq 0 ]; then
  echo $LINE | sed -e 's/CREATE/DROP/' -e 's/($/;/' >> resultFile
 fi
done << someFile

# 3  
Old 07-03-2003
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.

1. 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.

2. 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.
# 4  
Old 07-03-2003
Actually, the structure
Code:
while read someVariable
do
  stuff
done < fileName

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.

Last edited by oombera; 07-03-2003 at 05:30 PM..
# 5  
Old 07-03-2003
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.
# 6  
Old 07-08-2003
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.
Code:
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:
Code:
nawk 'match(t = toupper($0), /CREATE TABLE/) {
        t = substr(t,1,RSTART-1) "DROP" substr(t,RSTART+6)
        sub(/\(.*/, ";", t)
        print t
    }
    {print}' < infile > outfile

The backslash before the paren in the regexp may not be needed, depending on your version of nawk.

Note that both oombera's solution and mine create a second file, and do not, indeed can not, write the file in place.
# 7  
Old 07-10-2003
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.

Here is link to a useful BB for databases: www.dbforums.com.

MBB
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using commands within bash script

The purpose of enclosed script is to execute selected command and output success or failure in whiptail msgBox Works as expected when command returns zero to msgBox. I cannot figure out how to continue / automate my script when command expects reply to continue / terminate. I am doing it... (2 Replies)
Discussion started by: annacreek
2 Replies

2. UNIX for Dummies Questions & Answers

UNIX commands for a script that I need

Hello. I need help trying to create a script in UNIX to do the following steps. I don't know which commands to input to achieve this. 1. In a directory tree, I want to duplicate all .txt files into the same directory, so 2 of each file exists in each directory where there is a .txt file ... (4 Replies)
Discussion started by: TitanTlaloc
4 Replies

3. Shell Programming and Scripting

Issue commands within script

What is the best way of having a script that has it's own commands, which can be called at any stage in the script. I'm sure there is a technical term for this type of CLI app which can help my search but I can't remember it. Is using trap the best way of doing this. (6 Replies)
Discussion started by: cue
6 Replies

4. Shell Programming and Scripting

Commands in background in a script

Hi, I was writing a script for backup,however i stumbled upon this.( As i mentioned in my earlier posts iam a beginner in shell scripting). Here is a piece of code case $DB_STAT in OFFLINE) echo "Service $SID currently $DB_STAT" ... (1 Reply)
Discussion started by: maverick_here
1 Replies

5. Shell Programming and Scripting

How to make a script out of commands

Hi, I have a very basic knowledge of Unix and I need your help for a small script that will do the following commands that I do manually by just giving the filename TPR20080414.txt cut -d'|' -f3,4 TPR20080414.txt> oe_012.lkp awk -F "|" '{temp=$1;$1=$2;$2=temp}1' OFS="|" oe_012.lkp >... (3 Replies)
Discussion started by: sickboy
3 Replies

6. Shell Programming and Scripting

Can we use aliased commands in script?

Hi All, I need a small help.. when we use aliased commands in shell script, they are not being recognized when I used. Is there any way to use aliased commands in scritping? Please let me know if u know... Thank you Chanu (19 Replies)
Discussion started by: Chanakya.m
19 Replies

7. Shell Programming and Scripting

running commands from script

I'm new to unix and I have a fairly simple problem: Lets say I am in a specific directory and I run the command: "dirs" , I get an output of all the folders that i pushed into the stack (as expected), buut, when when I create a script (called test): #! /bin/csh dirs and then i run:... (2 Replies)
Discussion started by: owijust
2 Replies

8. Shell Programming and Scripting

script for ftp commands

Hi I need to setup a script to ftp a file from one unix box to another. I have modified the.netrc file, thus I can ftp into another box without being prompted for password. The question I have is, how can I setup a script which would after ftp, will change a directory, issue the bin command, then... (8 Replies)
Discussion started by: ali.merchant
8 Replies

9. Shell Programming and Scripting

Repeating commands in a script

I have written a script that I want to be repeated. The script that I wrote outputs the date, how many people are on the system, how many people logged in before me, and how many people logged in after me. Than what I want it to do is after it outputs the 4 lines I want it to go back to the... (4 Replies)
Discussion started by: Dave2874
4 Replies

10. UNIX for Dummies Questions & Answers

script commands

my bad (2 Replies)
Discussion started by: romeoz
2 Replies
Login or Register to Ask a Question