Vikram Vikram - 1 year ago 75
SQL Question

Extract table name from DDL File

I have DDL File and using grep command I can able to extract the below line using the command

cat employee.sql | grep -i 'TABLE'


DDL

CREATE TABLE TEMP_EMPLOYEE(
ID VARCHAR(255),
NAME VARCHAR(150),
ADDRESS VARCHAR(150),
STREET VARCHAR(150),
SUBURB VARCHAR(100),
STATE VARCHAR(100)
);


CREATE TABLE TEMP_EMPLOYEE(


I want to extract the just TABLE name further from the above string. Can some one help how. I've used
sed
but printing the same string.

Answer Source

I suggest to use GNU grep:

grep -Po 'CREATE TABLE \K[^(]*' employee.sql

or GNU sed:

sed -n 's/CREATE TABLE \(.*\)(/\1/p' employee.sql

or awk:

awk -F '[( ]' '/CREATE TABLE/ {print $3}' employee.sql

Output:

TEMP_EMPLOYEE
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download