LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 07-18-2012, 12:26 PM   #1
Slycraft
LQ Newbie
 
Registered: Jul 2012
Posts: 4

Rep: Reputation: Disabled
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
 
Old 07-18-2012, 12:38 PM   #2
Slycraft
LQ Newbie
 
Registered: Jul 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Bear with me here, I am a newb.

But my thoughts are something similar to :%s/^dn:/\r\nchangetype: add/g
 
Old 07-18-2012, 01:03 PM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
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
 
Old 07-18-2012, 01:14 PM   #4
Slycraft
LQ Newbie
 
Registered: Jul 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
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?
 
Old 07-18-2012, 01:28 PM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
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.
Old 07-18-2012, 02:09 PM   #6
Slycraft
LQ Newbie
 
Registered: Jul 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
PERFECT......druuna you have saved me countless hours of research....THANKS! and Thanks for the link!!!
 
Old 07-18-2012, 06:10 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,254

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
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.
 
Old 07-20-2012, 11:24 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] [bash - sed - awk] Match line with x characters and add string TigerClaw Linux - Newbie 4 02-28-2012 01:22 AM
PHP: Find a string in a text file and add something to that line aocferreira Programming 1 05-26-2011 02:52 PM
reg expr: match a string A-Z, a-z, * or a blank ( but string can not be all blanks) matt007 Programming 4 12-22-2009 09:55 AM
Script to insert string in first line of a file minil Programming 13 01-03-2006 12:56 AM


All times are GMT -5. The time now is 05:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration