To create a file writing a SQL into it from another file

Hi,

I have a file in which I have database information along with 1 SELECT statement. Only 1 statement would be there. I want to grep this SELECT STATEMENT only and write into a separate file.

Input File format:

Database_Name:<database>
Schema_Name:<schema>
Table_Name:<table>
Select * from <schema>.<table>

I have 2 ways, either I can use the 2nd and 3rd line to create the SELECT statament and write into a separate file:

Select * from <i'll extract schema name (2nd line) and table name (3rd line)>

or

I can grep SELECT statement mentioned in the 4th line and write into a separate file.

--------------------------------------------------------------------------

Other info:

Database is DB2

Let me know if more details are required.

Regards....

You don't need grep, tail will do to read lines including and after the fourth.

tail -n +4 filename

[edit] oh, you DO need the whole file. And the select statement isn't actually there.

( IFS=":"

read G DATABASE
read G SCHEMA
read G TABLE
echo "SELECT * FROM ${SCHEMA}.${TABLE}" ) < filename
$
$ cat f24
Database_Name:D1
Schema_Name:S1
Table_Name:T1
Select * from <schema>.<table>
$
$
$ perl -lne 'if (/^Schema_Name:(.*?)$/){$s=$1} elsif (/^Table_Name:(.*?)$/){$t=$1} elsif (/^(Select.*from).*$/){print "$1 $s.$t;"}' f24
Select * from S1.T1;
$
$

tyler_durden

hey thanks....it worked....

---------- Post updated at 02:54 PM ---------- Previous update was at 02:54 PM ----------

thanks to both of you.....