Help with loop in ksh script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with loop in ksh script
# 8  
Old 10-01-2014
I'm feeling a bit lost looking at your script, but are you really reading input_text.txt five times (twice in the while loops, and thrice with awk)? And do you intentionally overwrite the line variable in the inner loop? OK, you break out of the outer loop, but why, then, do it at all?
Your awk command all seem to search for similar conditions and then act on some variables - couldn't you do that in one awk script alone?

I'd propose you do some blueprint of the logics required and then start coding from scratch.
# 9  
Old 10-01-2014
Try One way to this using awk

Input

Code:
[akshay@nio tmp]$ cat infile
CREATE TABLE TAB1
(
COL1,
COL2
);

CREATE UNIQUE INDEX XPKTAB1
(
COL1
)TAB1;

CREATE TABLE TAB2
(
COL1,
COL2
);

CREATE UNIQUE INDEX XPKTAB2
(
COL2
)TAB2;

Script
Code:
[akshay@nio tmp]$ cat run.awk
function dothis(){ 
	if(s==2)
	{
		split(p,S,/;/); split(S[2],E,/[()]/) 
		gsub(/[()]/,"\n&",S[1]); gsub(/[(,]/,"&\n",S[1])
		printf("%sINDEX %s(%s);\n\nCOMMENT ON %s \042PK=,%s\042;\n\n", S[1],E[3],E[2],E[3],E[2]) 
		p = s = ""
	}
}
{
	dothis()
	s += /).*;/
	p  = p $0
} 
END{
	dothis()
}

How to run ?
Code:
[akshay@nio tmp]$ awk -f run.awk infile


Output

Code:
CREATE TABLE TAB1
(
COL1,
COL2
)INDEX TAB1(COL1);

COMMENT ON TAB1 "PK=,COL1";

CREATE TABLE TAB2
(
COL1,
COL2
)INDEX TAB2(COL2);

COMMENT ON TAB2 "PK=,COL2";

---------- Post updated at 04:48 PM ---------- Previous update was at 04:37 PM ----------

Note : I assume CREATE TABLE xxx comes first in your input file , CREATE UNIQUE INDEX xxx comes next and ends with )anything;.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh for loop

Any reason why this thing doesn't works in Korn Shell for (( expr1; expr2; expr3 )) do ..... ... repeat all statements between do and done until expr2 is TRUE Done Rgds, TS (4 Replies)
Discussion started by: targetshell
4 Replies

2. Shell Programming and Scripting

Aix .ksh for loop script.

Hi, I'm trying to write a for loop to run through a list of servers and for each server copy a file to a backup file. But I can't seem to get it to run through my server list. It work for individual servers, please see below. #!/bin/ksh SSH_USERID=khcuser webservers="server1 server2" ... (2 Replies)
Discussion started by: elmesy
2 Replies

3. Shell Programming and Scripting

explain while loop in ksh shell script

#!/bin/ksh log=ABCl log=EFG log=HIJ i=0 while <------ what is the meaning of ($i - lt 3) do print ${log} (( i=i+1 )) done (1 Reply)
Discussion started by: Bperl1967
1 Replies

4. Shell Programming and Scripting

Setting a variable in a while loop (.ksh script)

Hello Everyone, I'm still trying to grasp many concepts in .ksh scripting, one of them being variables inside loops. My problem is the following: * I'm trying to set a variable inside a while read loop to reuse it outside of said loop. My lines are the following :... (13 Replies)
Discussion started by: jimmy75_13
13 Replies

5. Shell Programming and Scripting

for loop in awk script using ksh

Guys, I am new in awk , I face problem while i try to use for loop in awk, I am using ksh, i am trying to set a for loop which runs as man times as the records in a file , the for loop like for(a=1;a<=5;a++) is working in my awk script but the one i need is not working :wall: for example ... (8 Replies)
Discussion started by: djahmed
8 Replies

6. Shell Programming and Scripting

KSH Script -- loop and data copy question

I am trying to write a script that will allow me to train others with commands that I run manually by only allowing the exact command before continuing onto the next set of commands. Here is where I come into an issue. I have changed the directories for this post. Software we run creates files... (2 Replies)
Discussion started by: hurtzdonut
2 Replies

7. Shell Programming and Scripting

For loop in ksh..Please help..

Hi ALL, I need to take some command line arguments for my script and then want to run a function for each argument.I thought of using for loop as below, but its not working , can some one please help... #!/bin/ksh lpar1=$1 lpar2=$2 lpar3=$3 lpar4=$4 lpar5=$5 echo "$lpar1" >>lpar.txt echo... (4 Replies)
Discussion started by: prashant43
4 Replies

8. Shell Programming and Scripting

import var and function from ksh script to another ksh script

Ih all, i have multiples ksh scripts for crontab's unix jobs they all have same variables declarations and some similar functions i would have a only single script file to declare my variables, like: var1= "aaa" var2= "bbb" var3= "ccc" ... function ab { ...} function bc { ... }... (2 Replies)
Discussion started by: wolfhurt
2 Replies

9. Shell Programming and Scripting

ksh while loop

hi all, got this silly problem and i just can't seem to make sense of the error message its is saying 1400: cannot open. its my first time at writing a while loop but tried all sorts to get it working without success. #!usr/bin/ksh integer max=1400 set file="afilename" integer i=1 ... (3 Replies)
Discussion started by: scriptingmani
3 Replies

10. Shell Programming and Scripting

ksh "while" loop within a csh script

I'm no unix pro for sure, but I have programmed enough other languages to usually get the job done in unix when I have to. I'm currently trying to automate a manual diagnostic process. One of the steps in the manual process generates a file of text output. The next step is running a small... (4 Replies)
Discussion started by: bschnair
4 Replies
Login or Register to Ask a Question