Announcement

Collapse
No announcement yet.

changing the contents of a file

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    [SOLVED] changing the contents of a file

    Hello All,

    I am pulling my already rapidly disappearing hair out on this one.
    I am working on an Oracle database clone script. As part of that I have to temporarily edit the tnsnames.ora file.
    It has a structure like this:
    Code:
    DBNAME=
            (DESCRIPTION=
                    (ADDRESS=(PROTOCOL=tcp)(HOST=db.acme.com)(PORT=1521))
                (CONNECT_DATA=
                    (SERVICE_NAME=ORCL)
                    (INSTANCE_NAME=ORCL1)
                )
            )
    In a tnsnames.ora file there are usually several entries like this so I must
    1) find the correct entry
    2) Navigate to the
    Code:
    (CONNECT_DATA=)
    line
    3) insert a new line
    4) on the new line add the following
    Code:
    (UR=A)
    5) save the file
    6) move on to the next step

    Can anyone point me to some sample code on how to do this in a bash script?

    THANK YOU!!!!!
    Thanks,

    Craigbert
    ----------------------------------------------------
    Kubuntu 13.04
    8GB RAM & Intel Core i7 1.87GHz
    Registered Linux User 537624

    #2
    sed can edit a file inplace with the -i option. For example:
    sed -e 's/\( *\)\((CONNECT_DATA=\)/\1\2\n\1 (UR=A)/' -i.bak tnsnames.ora
    will add the (UR=A/) line after each (CONNECT_DATA= line in the tnsnames.ora file and save the previous a backup copy to tnsnames.ora.bak

    [I tried saving the indentation with \( *\) in the search part and using \1 in the substitute part.]

    I am sure an efficient perl script could be written as well. This example uses sed.

    Hope this helps...

    Comment


      #3
      Thanks ~ that is pretty slick!
      The only problem with this is that it adds the (UR=A) to ALL the CONNECT_DATA sections.
      I need to only do it for the one entry that starts with DBNAME=
      Is there a way to modify that line so that only one of the CONNECT_DATA sections is updated?
      Thanks,

      Craigbert
      ----------------------------------------------------
      Kubuntu 13.04
      8GB RAM & Intel Core i7 1.87GHz
      Registered Linux User 537624

      Comment


        #4
        Since the context of the line matters, I would probably use awk instead. Unfortunately, awk does not do in-place editing. Move the file first, then run awk. Like this:
        Code:
        mv tnsnames.ora tnsnames.ora.bak; awk -f awk-script tnsnames.ora.back > tnsnames.ora
        Now, the content of awk-script looks something like this:
        Code:
        BEGIN { in_dbname = 0; }
        
        /^[A-Z]+/ { in_dbname = 0; }
        /^DBNAME=.*/ { in_dbname = 1; }
        
        { print $0; }
        / +\(CONNECT_DATA/ {
            if (in_dbname) print "                (UR=A)";
        }
        This sets a flag when ever a line begins with DBNAME= and clears the flag if the line starts with some other variable. All lines are printed as is, if (CONNECT_DATA is found in the right context, it adds the (UR=A) line after it.

        Comment


          #5
          Two days I have spent reading and trying and failing and you solved it in 10(?) minutes.
          I bow in your general direction!



          THANK YOU!!!!
          Thanks,

          Craigbert
          ----------------------------------------------------
          Kubuntu 13.04
          8GB RAM & Intel Core i7 1.87GHz
          Registered Linux User 537624

          Comment


            #6
            For automated file editing, you go to the spa (Sed, Perl, Awk)

            Comment

            Working...
            X