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, ...
- 06-25-2009 #1Just 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
- 06-25-2009 #2Linux User
- Join Date
- Aug 2006
- Posts
- 458
i don't understand. Show your final output. Show an exact sample of your input.
- 06-26-2009 #3Just 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
- 06-26-2009 #4Linux 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


Reply With Quote