Here is another way with GNU awk
:
awk '
BEGIN{IGNORECASE=1}
/create/&&/table/{for(i=1;i<=NF;i++) if($i~/[.]/){split($i,tmp,/\./); print tmp[2]}}' ddl.txt
Output:
TABLE1
dummy
Question
I have following string in a file called ddl.txt
CREATE SET TABLE DB.TABLE1 ,NO FALLBACK ,
NO BEFORE JOURNAL (
key DECIMAL(18,0);
drop table DB.dummy;
create table DB.dummy (id varchar(10));
I need to extract table names in above file as follows which contain key words 'TABLE' & 'CREATE' but in different pattern, (Expected Output)
TABLE1
dummy
what i tried is,
a=`cat ddl.txt`
a=`echo "$a" | tr [a-z] [A-Z]`
echo "$a" | awk -v RS=, '/TABLE/&&/CREATE/{print $NF}' | awk -F'.' '{print $2}'
This is returning only
TABLE1
I need the other table name dummy
which is in different pattern search.
Note: Always we should check the pattern is matching with keywords TABLE & CREATE as i used in above query; also DB is dynamic ... it is not always DB
No correct solution
OTHER TIPS
Here is another way with GNU awk
:
awk '
BEGIN{IGNORECASE=1}
/create/&&/table/{for(i=1;i<=NF;i++) if($i~/[.]/){split($i,tmp,/\./); print tmp[2]}}' ddl.txt
Output:
TABLE1
dummy
Will this help?
$ cat t.awk
/CREATE.*TABLE/{table1=$5}
/create table/{table2=$4}
END{
print table1
print table2
}
$ awk -F'[ .]' -f t.awk input.txt
TABLE1
dummy
Update
Assume the folllowing input:
CREATE SET TABLE DB.TABLE1 ,NO FALLBACK ,
NO BEFORE JOURNAL (
key DECIMAL(18,0);
drop table DB.dummy;
create table DB.dummy (id varchar(10));
CREATE SET TABLE BD.TABLE2 ,NO FALLBACK ,
NO BEFORE JOURNAL (
key DECIMAL(18,0);
drop table BD.dummier;
create table BD.dummier (id varchar(10));
awk:
/CREATE.*TABLE/{
t1[n++] = $5
}
/create table/{
t2[k++] = $4
}
END{
for (i=0; i<=n; i++) {
print t1[i], t2[i]
}
}
Output:
$ awk -F'[ .]' -f t.awk input.txt
TABLE1 dummy
TABLE2 dummier
If I get what you meant, I would do this :
$ cat a
CREATE SET TABLE DB.TABLE1 ,NO FALLBACK ,
NO BEFORE JOURNAL (
key DECIMAL(18,0);
drop table DB.dummy;
create table DB.dummy (id varchar(10));
$ kw="DB." # you can set kw according to what happens previously
$ awk -F"$kw" '(($0~/table/ || $0~/TABLE/) && ($0~/create/ || $0~/CREATE/)){print $2}' a | awk '{print $1}'
TABLE1
dummy
I assume the name are always after "DB." pattern