Find the answer to your Linux question:
Results 1 to 4 of 4
Hello I am looking to have a script that performs some tasks for find and replace and inserts a line as well. I have done some programmign 10 years ago, ...
  1. #1
    Just Joined!
    Join Date
    Jun 2009
    Posts
    2

    multiline multirecord find and replace script

    Hello

    I am looking to have a script that performs some tasks for find and replace and inserts a line as well. I have done some programmign 10 years ago, so it is causing me a little grief.

    File consists of 2500 records. I will show you a sample consisting of three records and what needs to be replaced and inserted:

    dn: cn=Marquisd,ou=PTG,ou=MTL,ou=QUE,o=ASDASD
    changetype: modify
    ndsHomeDirectory: cn=APMMTL02F25_MRC1,ou=SVR,ou=MTL,ou=QUE,o=ASDASD# 0#USAGERS\
    TVG\Chagnonl

    dn: cn=USERNAME1,ou=PTG,ou=MTL,ou=QUE,o=ASDASD
    changetype: modify
    INSERT LINE HERE WRITTEN AS SUCH
    ndsHomeDirectory: cn=APMMTL02F25_MRC1,ou=SVR,ou=MTL,ou=QUE,o=ASDASD# 0#USAGERS\
    TVG\REPLACEHERE


    basically each of the 2 above are the records. There are 2500 of them to be done.
    1) I guess to cut the first line and extract the variable needed eg (Marquisd)

    2)locate the end of the second line and insert the extra line to be added
    3) locate the text on the end line (after the last slash) and replace this data with the entry obtained from the varialbe above
    4) repeat for all the records, and end

    Thanks!
    JAMIE

  2. #2
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    i don't understand. Show your final output. Show an exact sample of your input.

  3. #3
    Just Joined!
    Join Date
    Jun 2009
    Posts
    2

    exact input and putput

    here it is

    EXACT INPUT (these are 2 different records in a text file)

    cn=Marquisd,ou=PTG,ou=MTL,ou=QUE,o=ASDASD
    changetype: modify
    ndsHomeDirectory: cn=APMMTL02F25_MRC1,ou=SVR,ou=MTL,ou=QUE,o=ASDASD# 0#USAGERS\
    TVG\Chagnonl

    dn: cn=USERTWO1,ou=PTG,ou=MTL,ou=QUE,o=ASDASD
    changetype: modify
    INSERT LINE HERE WRITTEN AS SUCH
    ndsHomeDirectory: cn=APMMTL02F25_MRC1,ou=SVR,ou=MTL,ou=QUE,o=ASDASD# 0#USAGERS\
    TVG\Chagnonl

    NEEDED OUTPUT

    cn=Marquisd,ou=PTG,ou=MTL,ou=QUE,o=ASDASD
    changetype: modify
    ADD A LINE OF TEXT HERE BLAH BLAH BLAH
    ndsHomeDirectory: cn=APMMTL02F25_MRC1,ou=SVR,ou=MTL,ou=QUE,o=ASDASD# 0#USAGERS\
    TVG\Marquisd

    dn: cn=USERTWO,ou=PTG,ou=MTL,ou=QUE,o=ASDASD
    changetype: modify
    ADD A LINE OF TEXT HERE BLAH BLAH BLAH
    ndsHomeDirectory: cn=APMMTL02F25_MRC1,ou=SVR,ou=MTL,ou=QUE,o=ASDASD# 0#USAGERS\
    TVG\USERTWO

    TO SUMMARIZE:

    1) TAKE DATA FROM LINE BETWEEN CN= and first comma
    2)add a line of text after second line
    3) take entry above (step1) and edit over entry on line after \USAGERS\TVG

    THX

  4. #4
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Code:
    awk 'BEGIN{ FS="[,=]"}
    /^[dc]n/{ name=$2}
    /modify$/{ 
     print
     print "ADD A LINE OF TEXT HERE BLAH BLAH BLAH"
     next
    }
    /ndsHomeDirectory/{gsub(/TVG.*$/,"TVG\\"name)}
    1' file

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...