Linux - Newbie This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
|
07-18-2012, 12:26 PM
|
#1
|
LQ Newbie
Registered: Jul 2012
Posts: 4
Rep:
|
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"
Goal
"dn: blah blah blah
changeType: add
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.
Thanks for the help guys
|
|
|
07-18-2012, 12:38 PM
|
#2
|
LQ Newbie
Registered: Jul 2012
Posts: 4
Original Poster
Rep:
|
Bear with me here, I am a newb.
But my thoughts are something similar to :%s/^dn:/\r\nchangetype: add/g
|
|
|
07-18-2012, 01:03 PM
|
#3
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
From within vi:
Code:
:%s/dn: blah blah blah/dn: blah blah blah\rchangeType: add/
But why use vi in the first place?
Code:
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
|
|
|
07-18-2012, 01:14 PM
|
#4
|
LQ Newbie
Registered: Jul 2012
Posts: 4
Original Poster
Rep:
|
hmm think we are close to the same page here. Here is some more complete data.
CURRENTLY
dn: CN=SourceControl, OU=Security Groups, OU=MyBusiness, DC=ec,DC=loc
objectCategory: CN=Group,CN=Schema,CN=Configuration,DC=ec,DC=loc
dn: CN=Executive, OU=Security Groups, OU=MyBusiness, DC=ec,DC=loc
objectCategory: CN=Group,CN=Schema,CN=Configuration,DC=ec,DC=loc
WHAT IT SHOULD LOOK LIKE
dn: CN=SourceControl, OU=Security Groups, OU=MyBusiness, DC=ec,DC=loc
changeType: add
objectCategory: CN=Group,CN=Schema,CN=Configuration,DC=ec,DC=loc
dn: CN=Executive, OU=Security Groups, OU=MyBusiness, DC=ec,DC=loc
changeType: add
objectCategory: CN=Group,CN=Schema,CN=Configuration,DC=ec,DC=loc
-------------------------
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".
Sound right?
|
|
|
07-18-2012, 01:28 PM
|
#5
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
Sed:
Code:
sed '/^dn:/a changeType: add' infile
sed 's/^dn:\(.*\)$/dn:\1\nchangeType: add/' infile
Vi:
Code:
:%s/dn:\(.*\)/dn:\1\rchangeType: add/
Last 2 of these use back-referencing. All that is matched between \( and \) can be represented by \1 in the replace part ( http://www.thegeekstuff.com/2009/10/...tion-examples/)
Last edited by druuna; 07-18-2012 at 01:30 PM.
|
|
1 members found this post helpful.
|
07-18-2012, 02:09 PM
|
#6
|
LQ Newbie
Registered: Jul 2012
Posts: 4
Original Poster
Rep:
|
PERFECT......druuna you have saved me countless hours of research....THANKS! and Thanks for the link!!!
|
|
|
07-18-2012, 06:10 PM
|
#7
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,415
|
BTW, you could also use a vim macro http://vimdoc.sourceforge.net/htmldoc/usr_10.html#10.1
Basically doing it once manually, recording as you go, then re-run as many times as you want.
|
|
|
07-20-2012, 11:24 AM
|
#8
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852
|
Just to give you more options, here's the same command in ed too.
Code:
printf '%s\n' 'g/^dn:/a\' 'changeType: add' ',p' | ed -s file.txt
" ,p" prints the altered output to stdout. Change it to " w" to write them back to the input file.
How to use ed:
http://wiki.bash-hackers.org/howto/edit-ed
http://snap.nlc.dcccd.edu/learn/nlc/ed.html
(also read the info page)
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.
|
|
|
All times are GMT -5. The time now is 03:05 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|