Help with loop in ksh script

Hi, I am new to UNIX. I am working on a script where it takes the input and produces a desired output and it works fine for one instance.

Input(One Instance):

CREATE TABLE TAB1
(
COL1,
COL2
);

CREATE UNIQUE INDEX XPKTAB1
(
COL1
)TAB1;

Output:

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

COMMENT ON TAB1 "PK=,COL1";

But if I try to execute the same script for multiple instances such as

CREATE TABLE TAB1
(
COL1,
COL2
);

CREATE UNIQUE INDEX XPKTAB1
(
COL1
)TAB1;

CREATE TABLE TAB2
(
COL1,
COL2
);

CREATE UNIQUE INDEX XPKTAB2
(
COL2
)TAB2;

I am getting the output like below

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

COMMENT ON TAB1 "PK=,COL1";
COMMENT ON TAB2 "PK=,COL2";

But the actual output should be something like this.

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";

Can you please advise how I can achieve this.

Thanks,
V

What have you tried?

Well i would suggest go and pick a book and learn basic shell scripting. We are not here to baby sit and teach you very basics of unix shell scripting. I saw your other post earlier and now this a continuation of that. You cant rely on the forum for completing your task, what ever you are trying to achieve. What you are asking here is very basic , trying to write a loop and it is first step in logical programming and nothing to do with unix as well.

I used the below loop condition

WHILE IFS=read -r line
do

#Complete Code for one instance as explained above

done < input.txt

When I executed the above, it kept on running by overwriting the output and re running again from the start. So, I had to kill the job.

Thanks,
V

That's a start. Now, what did you put above, below, and inside that loop?

We can help fix your code, we can't do absolutely everything for you.

A few points already:

Watch out for upper cases. The commands and statements are case sensitive, therefore WHILE is not the same that While or while. By the way the correct one is `while' all lower case.

IFS=read should be IFS= read with space after the `='

If this is not actually the way you have it in your script, please, post the unchanged relevant portion of the script.

Here is the code

#!/bin/ksh

while IFS= read -r line
do


export create_txt="CREATE TABLE";
export END_TXT=");";
export OUTPUT_TXT=output.txt;
rm -f output.txt;
while read line
do

temp1=`echo $line | cut -d" " -f1,2`;
TBL_NM=`echo $line | cut -d" " -f3`;

if [[ $temp1 == $create_txt ]]
then
  echo "CREATE TABLE ${TBL_NM}" >> ${OUTPUT_TXT};
  else if [[ $line == $END_TXT ]]
  then
    echo ") INDEX " >> ${OUTPUT_TXT};
awk '/CREATE UNIQUE INDEX/,/)/ {
 if(/CREATE UNIQUE INDEX|\(/) {
  next
 }
 if (!/\)/){
  A=A $NF
 }
 else{
  X=$NF
  gsub(/TABLE_|;/,"",X)
  print "" X "_PI"
 }
}' input_text.txt >> ${OUTPUT_TXT};
awk '/CREATE UNIQUE INDEX/,/)/ {
 if(/CREATE UNIQUE INDEX|\(/) {
  next
 }
 if (!/\)/){
  A=A $NF
 }
 else{
  X=$NF
  gsub(/TABLE_|;/,"",X)
  print "(" A "); \n \n"
 }
}' input_text.txt >> ${OUTPUT_TXT};


awk -v '/CREATE UNIQUE INDEX/,/)/ {
 if(/CREATE UNIQUE INDEX|\(/) {
  next
 }
 if (!/\)/){
  A=A $NF
 }
 else{
  X=$NF
  sub(/;/,"",X)
  print "COMMENT ON TABLE " X " AS \047PK=," A "; \047; \n \n"
 }
}' input_text.txt >> ${OUTPUT_TXT};


done < input_text.txt

break;

done < input_text.txt

---------- Post updated at 07:58 PM ---------- Previous update was at 07:55 PM ----------

Aia,
Sorry, it was a typo...I used "while" in small letters and also used space between '=' and 'read'.

Thanks,
V

1 Like

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.

Try One way to this using awk

Input

[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

[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 ?

[akshay@nio tmp]$ awk -f run.awk infile

Output

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