Help with loop in ksh script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with loop in ksh script
# 1  
Old 09-30-2014
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):
Code:
CREATE TABLE TAB1
(
COL1,
COL2
);

CREATE UNIQUE INDEX XPKTAB1
(
COL1
)TAB1;

Output:
Code:
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

Code:
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

Code:
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.

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

Can you please advise how I can achieve this.

Thanks,
V
# 2  
Old 09-30-2014
What have you tried?
# 3  
Old 09-30-2014
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.
# 4  
Old 09-30-2014
I used the below loop condition

Code:
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
# 5  
Old 09-30-2014
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.
# 6  
Old 09-30-2014
Quote:
Originally Posted by varun2327
I used the below loop condition

Code:
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
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.
# 7  
Old 09-30-2014
Here is the code

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
This User Gave Thanks to varun2327 For This Post:
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