Sorting lines based on keywords for MySQL script

the thing which i require is very very complex.. i tried hard to find the solution but couldnt..
the thing i need to achieve is say i have a file

 
cat delta.sql
CREATE VIEW Austin
Etc etc
.
.
.
CREATE VIEW Barabara
AS
SELECT blah blah blah
FROM Austin z, Cluster s, Instance i
WHERE s. ZoneId = z.id AND z. InstanceId = i.id;
CREATE VIEW Chester
AS
SELECT z.id Id, z.name name, s. vip, i.name Name
FROM Barbara z, Cluster s, Instance i
WHERE s. ZoneId = z.id AND z. InstanceId = i.id;
 
CREATE VIEW Zithorn
AS
SELECT z.id Id, blah blah blah
FROM Austin z, Cluster s, Instance i
WHERE s. ZoneId = z.id AND z. InstanceId = i.id;

The above order works because Barbara and Zithorn creates view from Austin and Chester creates view
from Barabara�. The think I want to convey is the views access another view once its already created�.
The thing where I am stuck is ..

 
Cat delta.sql
CREATE VIEW Austin
Etc etc
.
.
.
CREATE VIEW Chester
AS
SELECT z.id Id, z.name name, s. vip, i.name Name
FROM Barbara z, Cluster s, Instance i
WHERE s. ZoneId = z.id AND z. InstanceId = i.id;
CREATE VIEW Barabara
AS
SELECT blah blah
FROM Austin z, Cluster s, Instance i
WHERE s. ZoneId = z.id AND z. InstanceId = i.id;
 
CREATE VIEW Zithorn
AS
SELECT z.id Id, blah blah blah
FROM Austin z, Cluster s, Instance i
WHERE s. ZoneId = z.id AND z. InstanceId = i.id;

Here observe that Chester tries to access from Barbara but since the file flows in order
during execution, it will give an error cause when it flows in order and executes view
Chester it tries to verify whether there is a view called Barbara. since its not present at the
moment (only present after this view or few lines below this view) it will give an error�
so I need a script which finds this type of bug and writes it in order that is somewhat like sorting�
I have come up so far as below

 
while read line
do
                if grep -q -i "create view" <<<$line
                  then
                firstword="$line"
#extracting the a view in a single file viewextract1.sql
                sed -n "/$firstword\$/,/;/p" delta.sql >viewextract1.sql
 
                view_name1=`echo $firstword | awk -F' ' '{print $3}'`
                fromView=$( cat  viewextract1.sql | grep "FROM" | grep 'FROM' | awk '{print $2}' )
#now I don�t know what logic  I can use here from here on to sort the views accordingly.. L L
#one way I thought of doing it is to find the line no of fromView which is present in a line 
�create view $fromView� and also find the line no of current view and if the $fromView in �cerate view� 
 line is greater than the line no of current view then cut paste the �create view $fromView� just above 
current view� 
          fi
 
done < delta.sql

Based on the input given .. Try with this ..

$ cat file_to_run
for i in $(nawk '/CREATE VIEW /{print $NF}' delta.sql)
do
        parent_view=$(sed -n "/CREATE VIEW $i/,/id;/p" delta.sql | nawk '/FROM/{print $2}')
        sed -n "/CREATE VIEW $i/q;p" delta.sql | grep "CREATE VIEW $parent_view" > /dev/null
        [ $? -eq 0 ] && echo "$i --> Success" || echo "$i --> Failure"
done < delta.sql
$
$ ./file_to_run
Austin --> Failure
Chester --> Failure
Barabara --> Success
Zithorn --> Success
$
1 Like
awk '/CREATE VIEW/{x=$NF;A[x]=NR}/FROM/{B[x]=(($2 in A)?"success":"fail")}END{for(i in A) print i"--->"B}' yourfile
2 Likes

Awesome .. :slight_smile:

thanks jayan_jay & ctsgnb both the code worked for me:-)