Shell script using loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script using loop
# 15  
Old 07-09-2010
Code:
$
$
$ # display the content of the file "name"
$ cat name
tbla
tblc
$
$ # display the content of the file "temp"
$ cat temp
ALTER TABLE tbla
ADD  ( cola1         VARCHAR2(10),
       cola2  VARCHAR2(11)  );

ALTER TABLE tblb
ADD  ( colb1         VARCHAR2(12),
       colb2  VARCHAR2(13)  );

ALTER TABLE tblc
ADD  ( colc1         VARCHAR2(14),
       colc2  VARCHAR2(15)  );
$
$
$ # now run the Perl one-liner
$ perl -ne 'chomp; $fmt = "%-10s  %-10s  %s\n";
>           if ($ARGV eq "name") {$x{$_}++}
>           elsif (/^ALTER TABLE (.*)/ and defined $x{$1}) {$in=1; $tbl=$1}
>           elsif ($in and /^ADD\s*\(\s*(\w+)\s*(.*?),/) {printf($fmt,$tbl,$1,$2)}
>           elsif ($in and /^\s*(\w+)\s*(.*?)\s*\);/) {printf($fmt,$tbl,$1,$2)}
>           elsif (/^\s*$/) {$in=0}
>          ' name temp
tbla        cola1       VARCHAR2(10)
tbla        cola2       VARCHAR2(11)
tblc        colc1       VARCHAR2(14)
tblc        colc2       VARCHAR2(15)
$
$
$

tyler_durden
# 16  
Old 07-09-2010
Quote:
Originally Posted by alisha
hi
i have used /usr/xpg4/bin/awk and nawk
in both cases getting error.

---------- Post updated at 08:00 AM ---------- Previous update was at 07:53 AM ----------

hi i have used your code starting with /usr/xpg4/bin/awk its giving me syntax error near lines.

Code:
/usr/xpg4/bin/awk: file "[command line]": line 16: syntax error  Context is:
>>>
>>>
>>>     next ;
>>>     /^ADD/, <<<

Seems thatr there is a missing line in your script :
Code:
   next ;                               #   Proceed next record
}

/^ADD/,/;[[:space:]]*$/ {               # Select ADD records (from ADD to

Jean-Pierre.
This User Gave Thanks to aigles For This Post:
# 17  
Old 07-12-2010
Thanks alot AIGLES.its working now.
# 18  
Old 07-16-2010
Hi aigles

Thanks for the help.I need one more favor.I have modified the code according to my need but now need to add one more condition.while we are searching for line having "add"can we do somthing to first search for line containig "add constraint" and print constraint name in the output of table and column names.Then search for line that contains only "ADD" not the " ADD CONSTRAINT" and print the output.
# 19  
Old 07-16-2010
Please post an example of the input file containing ADD CONSTRAINT.

Jean-Pierre.
# 20  
Old 07-19-2010
I need to do all the modification in previous code.

Here firstly i need to check whether
the table exits or not.If exists then searching for line having &quot;ADD constraint&quot; and
need to print the constraint name.
Now if add constraintnot does not exist then need to check for &quot;ADD ( column name&quot; as
we had previously done.Input for &quot;ADD CONSTRAINT&quot; is given below:-


ALTER TABLE PREMIUM_MSGS_SDB ADD CONSTRAINT PK_PREMIUM_MSGS_SDB PRIMARY KEY (MSG_TYPE,COUNTRY_CODE,LEG_VEH);(CODE/)

One more thing in the coding part we are working with a defined format of &quot;alter table&quot;
that is like below:

ALTER TABLE tbla
ADD ( cola1 VARCHAR2(11),
cola2 VARCHAR2(12) );(CODE/)

I want to add some command in my code so that the script will work on all formats.
Is it possible?
script which i needto modify:

#!/bin/sh
#read.sh
sed -n '/ALTER/,/;/p' new.txt > data.txt
sed '/;/ a\
' data.txt > data.ex
/usr/xpg4/bin/awk '
NR==FNR {
tbl[$1]++;
next;
}
/ALTER TABLE/ {
tbl_name = $3;
if (tbl_name in tbl)
getcols = 1
else getcols = 0;
next;
}

getcols != 1 {
printf &quot;%-12s\t %-12s\t %-12s\t %s\t\t %s\n&quot; , tbl_name , &quot;N/A&quot; , &quot;N/A&quot; , &quot;N/A&quot; , &quot;NIL&quot;;
next ;
}
/^ADD/,/;[[:space:]]*$/ {
f = ($1 ~ /^ADD/ ? 3 : 1);
sub(/).*/, &quotSmilie&quot;, $(f+1));
printf &quot;%-12s\t %-12s\t %-12s\t %s\t %s\n&quot; , tbl_name, $f, $(f+1) , &quot;NOT NULL&quot; ,&quot;Y&quot;;

}

' table data.ex > result
echo &quot;
1i
&quot;`grep &quot;DDCF No.&quot; new.txt`\n&quot;

TABLE_NAME\t\t COLUMN_NAME\t DATA_TYPE\t VALUE\t\t STATUS\n
.
wq
&quot; | ex -s result
uniq result > result1
cat result1(CODE/)

---------- Post updated at 12:15 AM ---------- Previous update was at 12:02 AM ----------

Sorry to all because my post is not in good format but i am helpless i am trying to edit it but its not working and also not wrapingthe codes

Last edited by alisha; 07-19-2010 at 02:13 AM..
# 21  
Old 07-19-2010
I am not sure to understand the required output.
Can you post the output for the 2 following sample data files:

Input file "name":
Code:
tbla
tblc

Input file "temp":
Code:
ALTER TABLE tbla
ADD  ( cola1  VARCHAR2(11),
       cola2  VARCHAR2(12)  );

ALTER TABLE tbla  ADD CONSTRAINT PK_tbla PRIMARY KEY (cola1);

ALTER TABLE tblb
ADD  ( colb1  VARCHAR2(21),
       colb2  VARCHAR2(22)  );

ALTER TABLE tblb ADD CONSTRAINT PK_tblb PRIMARY KEY (colb1,colb2);
ALTER TABLE tblc ADD CONSTRAINT PK_tblc PRIMARY KEY (colc0);

ALTER TABLE tblc
ADD  ( colc1  VARCHAR2(31),
       colc2  VARCHAR2(32),
       colc3  VARCHAR2(32)  );

ALTER TABLE tbld ADD CONSTRAINT PK_tbld PRIMARY KEY (cold0,cold2,cold3);

Jean-Pierre.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script use two variable in for loop

I want to create a shell script to add a user and modify its comment field mentioned in a file. 1. File value:- username comment field value xyz123 xyztesting abc123 abctesting def123 deftesting 2. i am using below loop to create user... (2 Replies)
Discussion started by: Anil
2 Replies

2. Shell Programming and Scripting

Speed up the loop in shell script

Hi I have written a shell script which will test 300 to 500 IPs to find which are pinging and which are not pinging. the script which give output as 10.x.x.x is pining 10.x.x.x. is not pining - - - 10.x.x.x is pining like above. But, this script is taking... (6 Replies)
Discussion started by: kumar85shiv
6 Replies

3. Shell Programming and Scripting

Loop in shell script

Hi Friends, I have a file. the content is as below: file1: /A/B/C/abc.txt 2013-07-28 13:50:00,2013-07-31 01:00:00,5,710 /A/B/C/xyz.txt 2011-09-21 18:30:00,2013-07-30 06:15:00,15,65135 2009-11-09 18:00:00,2011-09-02 09:00:00,5,12345 2013-07-28 13:50:00,2013-07-31 01:00:00,5,710 ... (2 Replies)
Discussion started by: vsachan
2 Replies

4. Shell Programming and Scripting

Help with the For loop shell script

Hi, I have multiple files in a directory. Each file will have a header.I have to check if any of the files has 0 rows other than the header then I have to delete the files. Here “ Empty file” in my case means a file has header information but no data. I have to delete such files. If the file... (2 Replies)
Discussion started by: ganesnar
2 Replies

5. Shell Programming and Scripting

Avoiding For Loop in Shell Script

I am looking to a solution to the following problem. I have a very large file that looks something like this: Each group of three numbers on each line are three probabilities that sum to one. I want to output the maximum for each group of three. So desired output would be: or... (6 Replies)
Discussion started by: hydrabane
6 Replies

6. Shell Programming and Scripting

Help with IF statement with loop Shell Script

Hello I am very new to shell and I bought some books and trying to learn it. I started trying to write a script that will take a number and count it down to 1 with commas in between. This number can only be one argument. If lower than one or higher than one argument it sends an error message. ... (4 Replies)
Discussion started by: zero3ree
4 Replies

7. Shell Programming and Scripting

while loop problem in c shell script

Hi all, i write a script c shell set i = 1 while ( $i <= $#array ) echo "$array" @ i++ end i want to set it to i = i +2 in that statement . Can anybody help me? ---------- Post updated at 02:46 PM ---------- Previous update was at 02:35 PM ---------- anybody not how to solve it??? (2 Replies)
Discussion started by: proghack
2 Replies

8. Shell Programming and Scripting

Loop in shell script

Dear experts, i am quite new to shell script please any one can help me in this regard i would like write a script which takes input in the form >./Test.sh a,10,b,20,c,30... in this way i can give input in any number which is not constant in the end through loop i want to... (3 Replies)
Discussion started by: vin_pll
3 Replies

9. Shell Programming and Scripting

Help with loop in a shell script

I just want to write a little script, that reads the lines from a file, echos somthing in a new tmp.file and then do some commands whith the tmp.files. while read -r line do echo "TEST=" > tmp.$$ echo "$line" >> tmp.$$ any_command < tmp.$$ done < $INPUTFILE But I think I have to... (2 Replies)
Discussion started by: elifchen
2 Replies

10. Shell Programming and Scripting

If then else loop in Shell script

Hi Following is the code . When I give input as Bangalore,its dospalying Welcome to Hitech City. But say , if I select Delhi or US, its not displaying the corresponding message. Its still says Welcome to Hitech City. Seems that it not entering in the elif part. Please suggest. #!... (4 Replies)
Discussion started by: pankajkrmishra
4 Replies
Login or Register to Ask a Question