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
-----

# 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
-----

# 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

Perhaps you could adapt this...

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...

# 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--->    )
  )

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

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

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...

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

Many thanks for your help.

This works perfect.

Regards

dnicky