How to use Vi to match a string of text, add a new line, insert string...
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to use Vi to match a string of text, add a new line, insert string...
Hi Guys,
So I need to know how to Match a given string of text (at the begining of a line), move to the end of the line, add a new line, then add a given string. Example Below:
Currently
"dn: blah blah blah
objectCategory: CN=Group,CN=Schema,CN=Configuration,DC=ec,DC=loc"
I am trying to modify a ldif dump. Thing is there is about 80 places I would need to insert "changeType: add". All lines beginning with "dn:" are unique and are specific to the security group that I am trying to import to my new SBS 2011 server.
sed 's/dn: blah blah blah/dn: blah blah blah\nchangeType: add/' infile
# or
sed '/dn: blah blah blah/a changeType: add' infile
After you check to see if this is the output you want you can re-run the command with the -i option added to make the changes in the infile ("in place")
Code:
sed -i '/dn: blah blah blah/a changeType: add' infile
The only unique identifier is the "dn:". Any line that starts with "dn:" is a new section. There is about 80+ sections and each with a whole lot more attributes in them. My thoughts are - Go to the end of the line that starts with "dn:", create a new line, add string "changeType: add".
And by the way, please use ***[code][/code] tags*** around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, bolding, colors, or other fancy formatting.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.