Reading specific contents from a file and appending it to another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading specific contents from a file and appending it to another file
# 1  
Old 09-30-2005
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 10:52 PM.. Reason: Added code tags for legibility.
# 2  
Old 10-03-2005
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--->    )
  )

# 3  
Old 10-03-2005
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  
Old 10-03-2005
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  
Old 10-04-2005
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  
Old 10-04-2005
Many thanks for your help.

This works perfect.


Regards

dnicky
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Appending content of a file to another file before a specific character

Hi there, i've got a file with this content $ cat file1 Matt Mar The other file has the same number of lines with this content: $ cat file2 20404=767294 23450=32427 is there a way with either using sed, awk or paste to insert the content of file1 before the "=" character? So... (3 Replies)
Discussion started by: nms
3 Replies

2. Shell Programming and Scripting

ksh Script, Reading A File, Grepping A File Contents In Another File

So I'm stumped. First... APOLOGIES... my work is offline in an office that has zero internet connectivity, as required by our client. If need be, I could print out my script attempts and retype them here. But on the off chance... here goes. I have a text file (file_source) of terms, each line... (3 Replies)
Discussion started by: Brusimm
3 Replies

3. Emergency UNIX and Linux Support

sed replace file contents by reading from another file

Hello, My input file1 is like this by tab-delimited chr1 mm10_knownGene stop_codon 3216022 3216024 0.000000 - . gene_id "uc007aeu.1"; transcript_id "uc007aeu.1"; chr1 mm10_knownGene CDS 3216025 3216968 0.000000 - 2 gene_id "uc007aeu.1"; transcript_id "uc007aeu.1"; ... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

4. UNIX for Advanced & Expert Users

Appending a files contents to the end of a specific file name in several directories

Here is my dir structure: /tmp/dave/myappend.txt /tmp/dave/dir1/test.txt /tmp/dave/dir2/test.txt /tmp/dave/dir3/test.txt /tmp/dave/dir4/test.txt I want to append the contents of myappend.txt to the end of each file with the name "test.txt" in all dirs in /tmp/dave/ I have tried this:... (2 Replies)
Discussion started by: bigd213
2 Replies

5. Shell Programming and Scripting

Reading file contents until a keyword

Hi Guys, I need to read a file until I find a blank line. and in the next iteration I want to continue reading from the line I find a keyword. For ex: my file looks like PDS_JOB_ALIAS CRITERIA_ITEM_TYPE PDS_JOB_CRITERIA_ITEM CRITERIA_ITEM_TYPE First I want to read the file... (2 Replies)
Discussion started by: infintenumbers
2 Replies

6. UNIX for Dummies Questions & Answers

issue on reading the file and appending date

Hi Am having issue on appending time stamp I know the exact file names in the directory like a.dat b.dat c.dat e.dat f.dat I want to read all these file names and append the timestamp to each files like a.dat.20090604,b.dat.20090604 and move to the different directory. ... (3 Replies)
Discussion started by: bobprabhu
3 Replies

7. Shell Programming and Scripting

Bash copy file contents into an existing file at a specific location

Hi all I need to copy the entire contents of one file into an existing file at a specific location. I know the exact line number where I need to put it. It appears I would use either sed or awk to do this, but I have been unsuccessful so far: File A line 1 line 2 line 3 line 4 ... (6 Replies)
Discussion started by: gshepherd7
6 Replies

8. Shell Programming and Scripting

Reading and printing one by one contents of a file

I have a file which has following contents: localhost_IP_SIP_1233026552455.xml localhost_IP_SIP_1233026552460.xml localhost_IP_SIP_1233026552467.xml localhost_IP_SIP_1233026552759.xml localhost_IP_SIP_1233026552969.xml localhost_IP_SIP_1233026552975.xml ... (2 Replies)
Discussion started by: Aditya.Gurgaon
2 Replies

9. Shell Programming and Scripting

Reading specific contents from 1 input files and appending it to another input file

Hi guys, I am new to AWK and unix scripting. Please see below my problem and let me know if anyone you can help. I have 2 input files (example given below) Input file 2 is a standard file (it will not change) and we have to get the name (second column after comma) from it and append it... (5 Replies)
Discussion started by: sksahu
5 Replies

10. UNIX for Dummies Questions & Answers

reading ,writing,appending ,manipulating a file.

Hi my prob statement is to create a new file or to append to the 1tst file the followign chages. File 1: txt file. portfolio No a b c d abc 1 Any Any Any charString cds 2 values values values charString efd 3 can can can charString fdg 4 come come come charString... (4 Replies)
Discussion started by: szchmaltz
4 Replies
Login or Register to Ask a Question