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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To create a file writing a SQL into it from another file
# 1  
Old 09-27-2010
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....
# 2  
Old 09-27-2010
You don't need grep, tail will do to read lines including and after the fourth.

Code:
tail -n +4 filename

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

Code:
( IFS=":"

read G DATABASE
read G SCHEMA
read G TABLE
echo "SELECT * FROM ${SCHEMA}.${TABLE}" ) < filename

# 3  
Old 09-27-2010
Code:
$
$ 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
# 4  
Old 09-27-2010
hey thanks....it worked....

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

thanks to both of you.....
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Create SQL DML insert statements from file using AWK or similar

Hi all. This is my first post on this forum. I've previously found great help in the huge knowledgebase that is here, but this time I have not been able to find a solution to my problem. I have a large text file that looks like this: typedef struct ABC_struct_nbr1_ { char attr1; /*... (0 Replies)
Discussion started by: Yagi Uda
0 Replies

2. Programming

create a spool file based on values passed from korn shell to sql script

this is my issue. 4 parameters are passed from korn shell to sql script. parameter_1= varchar2 datatype or no value entered my user. parameter_2= number datatype or no value entered my user. parameter_3= number datatype or no value entered my user. parameter_4= number datatype or no... (5 Replies)
Discussion started by: megha2525
5 Replies

3. Shell Programming and Scripting

How can I create a CSV file from PL/Sql in UNIX?

Can someone help me on creating a script that will manage/create a csv file from Pl/Sql using UNIX?Any advice is welcome,thank you so much,:) (2 Replies)
Discussion started by: Atrap
2 Replies

4. Shell Programming and Scripting

Need help in writing a script to create a new text file with specific data from existing two files

Hi, I have two text files. Need to create a third text file extracting specific data from first two existing files.. Text File 1: Format contains: SQL*Loader: Release 10.2.0.1.0 - Production on Wed Aug 4 21:06:34 2010 some text ............so on...and somwhere text like: Record 1:... (1 Reply)
Discussion started by: shashi143ibm
1 Replies

5. Shell Programming and Scripting

Writing sql results to file using ksh -nevermind

I'm having problems with writing my sql results to a file: sqlplus -S username/password@DB <<!! set echo off set verify off set showmode off set feedback off set timing off set linesize 250 set wrap off set pagesize 0 set newpage none set tab off set trimspool on set colsep... (1 Reply)
Discussion started by: avillanueva
1 Replies

6. Shell Programming and Scripting

How to use sql data file in unix csv file as input to an sql query from shell

Hi , I used the below script to get the sql data into csv file using unix scripting. I m getting the output into an output file but the output file is not displayed in a separe columns . #!/bin/ksh export FILE_PATH=/maav/home/xyz/abc/ rm $FILE_PATH/sample.csv sqlplus -s... (2 Replies)
Discussion started by: Nareshp
2 Replies

7. Shell Programming and Scripting

writing the output of SQL into one file

Hi All, Please help me writing the below script. I have two sql queries. 1. Select count(1),Client_id from TABLE_A group by Client_id; 2. Select count(1),Client_id from TABLE_B group by Client_id; I need the output of above two sql queries in a single file. The output 2nd query should be... (4 Replies)
Discussion started by: 46019
4 Replies

8. UNIX for Dummies Questions & Answers

Create directories from a sql file?

Hi, I have a sql file which has CREATE and INSERT commands in it. Basically, inside this file, a table will be created and the data will be inserted into the table. I was wondering if there is a way for me to create directories from this file? Thanks in advance (4 Replies)
Discussion started by: tezarin
4 Replies

9. Programming

Writing to a File using pl/sql

Hi I am new to using pl/sql on a unix platform and am having trouble writing to a file from within a block. Below is an example of the code that I have. I know that I need to use UTL_FILE to accomplish this; however, I keep getting errors. Can someone please help me? I am trying to create a... (1 Reply)
Discussion started by: stky13
1 Replies

10. UNIX for Advanced & Expert Users

Reading a file and writing the file name to a param file.

Hi All, Not sure if this would be in a dummies sectiin or advanced. I'm looking for a script if someone has doen something like this. I have list of files - adc_earnedpoints.20070630.txt adc_earnedpoints.20070707.txt adc_earnedpoints.20070714.txt adc_earnedpoints.20070721.txt... (1 Reply)
Discussion started by: thebeginer
1 Replies
Login or Register to Ask a Question