0

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

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

but in may case db should be used as key word as it may vary. we can use . but need to take if somewhere dot is coming ... please reframe the command accordingly
1

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

3 Comments

I may have n number of tables which is not fixed. can you give me a dynamic solution ?
yes if you update your question so that I understand what you need :-)
I updated question.. also DB is dynamic ... it is not always DB
1

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

6 Comments

but in may case db should be used as key word as it may vary. we can use . but need to take if somewhere dot is coming ... please reframe the command accordingly
If you know the keyword, you can pass it to awk instead of "DB."
sorry what i meant is db should not be used as key
why not? After the edit you can easily modify DB with the keyword you want.
DB is dynamic word; only CREATE and TABLE are static
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.