filtering and copying contains of a file using awk/sed


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers filtering and copying contains of a file using awk/sed
# 1  
Old 02-04-2009
filtering and copying contains of a file using awk/sed

Hello folks,
I have 2 files one( file1) contains the ddl for a view and file 2 contains the view defination/alias columns.
I want to merge the 2 into a third file using awk/sed as follows:

cheers !
Smilie
FILE1
-----
PROMPT FIRST_VIEW
CREATE OR REPLACE FORCE VIEW FIRST_VIEW
AS
SELECT col1, col2, col3 from anytable1
where col1='X');
PROMPT SECOND_VIEW
CREATE OR REPLACE FORCE VIEW SECOND_VIEW
AS
SELECT test1, test2 from table2
where test2='TEST');

--------------------------------------------------------------
FILE2
-----
Create View TESTSCHEMA.FIRST_VIEW(
COL1,
col2,
col3)
Create View TESTSCHEMA.THIRD_VIEW(
dir1,
dir2,
dir3)
Create View TESTSCHEMA.SECOND_VIEW(
test1,
test2)
=================================================
Output file
output file:
-----------
PROMPT FIRST_VIEW
CREATE OR REPLACE FORCE VIEW FIRST_VIEW
(
COL1,
col2,
col3)
AS
SELECT col1, col2, col3 from anytable1
where col1='X');
PROMPT SECOND_VIEW
CREATE OR REPLACE FORCE VIEW SECOND_VIEW
(
test1,
test2)
AS
SELECT test1, test2 from table2
where test2='TEST');
# 2  
Old 02-05-2009
Code:
#!/bin/ksh
#----------------------------------------------------------------------#
# Separate all the views into their own files.                         #
#----------------------------------------------------------------------#
cat file1 |
while read line ; do
  if [[ $line = *PROMPT* ]]; then
    set $line
    file_out=$2.data
    :>$file_out
  fi
  echo $line >> $file_out
done

#----------------------------------------------------------------------#
# Separate all the columns into their own files, with name association.#
#----------------------------------------------------------------------#
cat file2 |
while read line ; do
  if [[ $line = *TESTSCHEMA* ]]; then
    set `echo $line | sed -e 's/[^a-zA-Z_]/ /g'`
    file_out=$4.data.2
    print "(" >$file_out
    continue
  fi
  echo $line >> $file_out
done


#----------------------------------------------------------------------#
# Look for all the file1 files...                                      #
#----------------------------------------------------------------------#
/bin/ls -1 *.data |
while read file_nm ; do
#----------------------------------------------------------------------#
# Read first 2 lines...                                                #
#----------------------------------------------------------------------#
  head -2 $file_nm
#----------------------------------------------------------------------#
# Grab the stuff that was originally in file2...                       #
#----------------------------------------------------------------------#
  cat $file_nm.2
#----------------------------------------------------------------------#
# Grab the rest of the file1 stuff.                                    #
#----------------------------------------------------------------------#
  sed -n '3,$p' $file_nm
done |
  tee big_ol_file

No way could ~I~ do all that in an awk script....
But... there's the ksh solution.
# 3  
Old 02-05-2009
ur the man ! it worked great ! Thanks a lot I really appreciate it.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

File filtering using awk or sed

Hello Members, I have a file, having below contents: <KEYVALUE>$4,0,1,4,966505098999--&gt;RemoteSPC: 13 SSN: 146</KEYVALUE> <KEYVALUE>$4,123,1,4,966505050198--&gt;RemoteSPC: 1002 SSN: 222,Sec:RemoteSPC: 1004 SSN: 222</KEYVALUE> <KEYVALUE>$4,123,1,4,966505050598--&gt;RemoteSPC: 1002 SSN:... (9 Replies)
Discussion started by: umarsatti
9 Replies

2. Shell Programming and Scripting

Filtering data using uniq and sed

Hello, Does anyone know an easy way to filter this type of file? I want to get everything that has score (column 2) 100.00 and get rid of duplicates (for example gi|332198263|gb|EGK18963.1| below), so I guess uniq can be used for this? gi|3379182634|gb|EGK18561.1| 100.00... (6 Replies)
Discussion started by: narachaid
6 Replies

3. Shell Programming and Scripting

Awk/sed : help on:Filtering multiple lines to one:

Experts Good day, I want to filter multiple lines of same error of same day , to only 1 error of each day, the first line from the log. Here is the file: May 26 11:29:19 cmihpx02 vmunix: NFS write failed for server cmiauxe1: error 5 (RPC: Timed out) May 26 11:29:19 cmihpx02 vmunix: NFS... (4 Replies)
Discussion started by: rveri
4 Replies

4. Shell Programming and Scripting

Need help for filtering a file through awk script

Hello Folks, I am working on filtering a file having some special characters. Let's say for an example a file contains person name and phone number based on positions. First 5 characters name and next 10 characters phone number. My task is to , if there is any special character in phone number... (6 Replies)
Discussion started by: dinesh1985
6 Replies

5. Shell Programming and Scripting

copying file information using awk & grep

Hi, TASK 1: I have been using this code to print the information of files kept at "/castor/cern.ch/user/s/sudha/forPooja" in some text file name FILE.txt. rfdir /castor/cern.ch/user/s/sudha/forPooja | grep data | awk '{print "rfio:///castor/cern.ch/user/s/sudha/forPooja/"$9}' > FILE.txt ... (6 Replies)
Discussion started by: nrjrasaxena
6 Replies

6. Shell Programming and Scripting

copying a line from a file using sed

Hi All, I need to copy a specific line from a file to another file. lets suppose the line number 13 of a file when I am writing the line number explicitly.. its working fine sed -n '13p' afile > anotherfile but, when inside a script, i am getting the line number value inside a variable... (4 Replies)
Discussion started by: gotamp
4 Replies

7. Shell Programming and Scripting

sed command for copying the contents of other file replacing it another file on specifc pattern

We have 2 file XML files - FILE1.XML and FILE2.xml - we need copy the contents of FILE1.XML and replace in FILE2.xml pattern "<assignedAttributeList></assignedAttributeList>" FILE1.XML 1. <itemList> 2. <item type="Manufactured"> 3. <resourceCode>431048</resourceCode> 4. ... (0 Replies)
Discussion started by: balrajg
0 Replies

8. Shell Programming and Scripting

Filtering Issues Using sed and awk

Hi, I am currently using the sed and awk commands to filter a file that has multiple sets of data in different columns. An example of part of the file i am filtering is as follows; Sat Oct 2 07:42:45 2010 01:33:46 R1_CAR_12.34 Sun Oct 3 13:09:53 2010 00:02:34 R2_BUS_56.78 Sun... (4 Replies)
Discussion started by: crunchie
4 Replies

9. Shell Programming and Scripting

Sed filtering issue

The problem I have is that I have 23,000 records I need to sort through to pull out LEN: XXXX XX XX XX XX and NCOS: XXX entries from so I can insert them into a database. But some of my records include TYPE: ISDN, THE DN IS UNASSIGNED, or INVALID entries in between some records and I would like... (2 Replies)
Discussion started by: roachmmflhyr
2 Replies

10. Shell Programming and Scripting

awk and sed filtering

Goo afternoon Sir'sould like to ask your help reagrding in this scenario using sed and awk. ******************************************************** Host:CDRMSAPPS1 Operating System:Linux 2.6.9-42.ELsmp Machine Type:UNIX Host Type:Client Version:5.1... (2 Replies)
Discussion started by: invinzin21
2 Replies
Login or Register to Ask a Question