![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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)
)
)
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
)
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. |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
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
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---> )
)
|
|
#3
|
|||
|
|||
|
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 |
|
#4
|
|||
|
|||
|
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 |
|
#5
|
||||
|
||||
|
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
|
|
#6
|
|||
|
|||
|
Many thanks for your help.
This works perfect. Regards dnicky |
|||
| Google The UNIX and Linux Forums |