The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
search for the contents in many file and print that file using shell script cdfd123 Shell Programming and Scripting 3 10-07-2007 07:17 PM
Reading file names from a file and executing the relative file from shell script anushilrai Shell Programming and Scripting 4 03-10-2006 02:25 AM
Creating file contents using contents of another file ReV Shell Programming and Scripting 21 02-24-2006 07:25 AM
Reading specific part of file guptan UNIX for Dummies Questions & Answers 5 06-30-2005 08:23 AM
reading specific line from file cool_boss2121 Shell Programming and Scripting 10 04-26-2005 06:05 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 09-30-2005
Registered User
 

Join Date: Aug 2005
Posts: 11
Reading specific contents from a file and appending it to another file

Hi,

I need to write a shell script (ksh) to read contents starting at a specific location from one file and append the contents at specific location in another file. Please find below the contents of the source file that I need to read the contents from,

File 1
-----
Code:
# more $ORACLE_HOME/network/admin/listener.ora
# LISTENER.ORA Configuration File:$TNSADMIN/listener.ora
#
# Notes:
# 1) Add GLOBAL_DBNAME for OMS site recognition

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS_LIST =
        (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC))
      )
      (ADDRESS_LIST =
        (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
      )
    )
    (DESCRIPTION =
      (PROTOCOL_STACK =
        (PRESENTATION = GIOP)
        (SESSION = RAW)
      )
      (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 2481))
    )
  )

SID_LIST_LISTENER =
  (SID_LIST =
    (SID_DESC =
      (SID_NAME = PLSExtProc)
      (ORACLE_HOME = /appl/oracle/product/920)
      (PROGRAM = extproc)
    ) 
    (SID_DESC =
      (GLOBAL_DBNAME=i_livend_can)
      (SID_NAME = inst1)
      (ORACLE_HOME = /appl/oracle/product/920)
    )
    (SID_DESC =
      (GLOBAL_DBNAME=i_archnd_can)
      (SID_NAME = inst2)
      (ORACLE_HOME = /appl/oracle/product/920)
    ) 
  )
Now, I need to read the contents indicated in bold from the above file and append it at a specific location in a target file. The target file is listed below

File 2
-----
Code:
# more $ORACLE_HOME/network/admin/listener.ora
# LISTENER.ORA Configuration File:$TNSADMIN/listener.ora

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS_LIST =
        (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC))
      )
      (ADDRESS_LIST =
        (ADDRESS = (PROTOCOL = TCP)(HOST = abcd)(PORT = 1521))
      )
    )
    (DESCRIPTION =
      (PROTOCOL_STACK =
        (PRESENTATION = GIOP)
        (SESSION = RAW)
      )
      (ADDRESS = (PROTOCOL = TCP)(HOST = abcd)(PORT = 2481))
    )
  )

SID_LIST_LISTENER =
  (SID_LIST =
    (SID_DESC =
      (SID_NAME = PLSExtProc)
      (ORACLE_HOME = /appl/oracle/product/920)
      (PROGRAM = extproc)
    ) 
    (SID_DESC =
      (GLOBAL_DBNAME=inst10)
      (SID_NAME = inst10)
      (ORACLE_HOME = /appl/oracle/product/920)
    )
    (SID_DESC =
      (GLOBAL_DBNAME=inst9)
      (SID_NAME = inst9)
      (ORACLE_HOME = /oracle/product/920)
    )
    (SID_DESC =
      (GLOBAL_DBNAME=inst8)
      (SID_NAME = inst8)
      (ORACLE_HOME = /oracle/product/920)
    )
    (SID_DESC =
      (GLOBAL_DBNAME=inst7)
      (SID_NAME = inst7)
      (ORACLE_HOME = /oracle/product/920)
    )
    (SID_DESC =
      (GLOBAL_DBNAME=inst6)
      (SID_NAME = inst6)
      (ORACLE_HOME = /oracle/product/920)
    )
    (SID_DESC =
      (GLOBAL_DBNAME=inst5)
      (SID_NAME = inst5)
      (ORACLE_HOME = /oracle/product/920)
    )
    (SID_DESC =
      (GLOBAL_DBNAME=inst4)
      (SID_NAME = inst4)
      (ORACLE_HOME = /oracle/product/920)
    )
   here
  )
The contents read from the File 1 above needs to be placed in File 2 at the location indicated by the text here.

Any help with the above is highly appreciated.

dnicky

Last edited by Ygor; 10-02-2005 at 06:52 PM. Reason: Added code tags for legibility.
Reply With Quote
Forum Sponsor
  #2  
Old 10-02-2005
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,249
Perhaps you could adapt this...
Code:
awk '
    FNR==NR && NR>=33 && NR<=42 {
        buf = buf "#---Added this line--->" $0 ORS
    }
    FNR!=NR {
        if (FNR==65)
            printf buf
        print
    }
    ' file1 file2 > file3
Gives...
Code:
# more $ORACLE_HOME/network/admin/listener.ora
# LISTENER.ORA Configuration File:$TNSADMIN/listener.ora

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS_LIST =
        (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC))
      )
      (ADDRESS_LIST =
        (ADDRESS = (PROTOCOL = TCP)(HOST = abcd)(PORT = 1521))
      )
    )
    (DESCRIPTION =
      (PROTOCOL_STACK =
        (PRESENTATION = GIOP)
        (SESSION = RAW)
      )
      (ADDRESS = (PROTOCOL = TCP)(HOST = abcd)(PORT = 2481))
    )
  )

SID_LIST_LISTENER =
  (SID_LIST =
    (SID_DESC =
      (SID_NAME = PLSExtProc)
      (ORACLE_HOME = /appl/oracle/product/920)
      (PROGRAM = extproc)
    )
    (SID_DESC =
      (GLOBAL_DBNAME=inst10)
      (SID_NAME = inst10)
      (ORACLE_HOME = /appl/oracle/product/920)
    )
    (SID_DESC =
      (GLOBAL_DBNAME=inst9)
      (SID_NAME = inst9)
      (ORACLE_HOME = /oracle/product/920)
    )
    (SID_DESC =
      (GLOBAL_DBNAME=inst8)
      (SID_NAME = inst8)
      (ORACLE_HOME = /oracle/product/920)
    )
    (SID_DESC =
      (GLOBAL_DBNAME=inst7)
      (SID_NAME = inst7)
      (ORACLE_HOME = /oracle/product/920)
    )
    (SID_DESC =
      (GLOBAL_DBNAME=inst6)
      (SID_NAME = inst6)
      (ORACLE_HOME = /oracle/product/920)
    )
    (SID_DESC =
      (GLOBAL_DBNAME=inst5)
      (SID_NAME = inst5)
      (ORACLE_HOME = /oracle/product/920)
    )
    (SID_DESC =
      (GLOBAL_DBNAME=inst4)
      (SID_NAME = inst4)
      (ORACLE_HOME = /oracle/product/920)
    )
#---Added this line--->    (SID_DESC =
#---Added this line--->      (GLOBAL_DBNAME=i_livend_can)
#---Added this line--->      (SID_NAME = inst1)
#---Added this line--->      (ORACLE_HOME = /appl/oracle/product/920)
#---Added this line--->    )
#---Added this line--->    (SID_DESC =
#---Added this line--->      (GLOBAL_DBNAME=i_archnd_can)
#---Added this line--->      (SID_NAME = inst2)
#---Added this line--->      (ORACLE_HOME = /appl/oracle/product/920)
#---Added this line--->    )
  )
Reply With Quote
  #3  
Old 10-03-2005
Registered User
 

Join Date: Aug 2005
Posts: 11
Hi Ygor,

Many thanks for your response. Would appreciate if you could explain in brief the logic used as I'm not quite familiar with awk scripting.


Regards

dnicky
Reply With Quote
  #4  
Old 10-03-2005
Registered User
 

Join Date: Aug 2005
Posts: 11
Hi Ygor,

If I understand it correctly, your code reads the contents of the first file from line 33 onwards and then appends the same into file 2 from line 65 onwards.
The logic referring to the exact line number i.e. 33 for reading the contents from file 1 should work, but as far as appending the text in file 2 is concerned, it will not always be at line 65 as the contents of file 2 could be varying and hence the position. What I'm looking at is to append the text in file 2 right at the end (irrespective of number of lines in file 2) just inside the last closing paranthesis i.e. ')' in file 2. File 2 would always contain the closing paranthesis at the end and was wondering if we could use the same instead of hardcoding a specific line number to append the text.

Thanks for your time and your help is much appreciated.


Regards

dnicky
Reply With Quote
  #5  
Old 10-03-2005
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,249
The above code reads file1 from lines 33 to 42 and inserts before line 65 of file2. This modified code inserts before the last line of file2...
Code:
awk '
    FNR==NR && NR>=33 && NR<=42 {
        buf = buf "#---Added this line--->" $0 ORS
    }
    FNR!=NR {
        if (FNR==ins)
            printf buf
        print
    }
    ' file1 ins=$(wc -l < file2) file2 > file3
Reply With Quote
  #6  
Old 10-04-2005
Registered User
 

Join Date: Aug 2005
Posts: 11
Many thanks for your help.

This works perfect.


Regards

dnicky
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 10:20 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0