VI Help

revert01

Distinguished
Nov 13, 2006
1
0
18,510
hey fellas new member here :)

been looking all over the web for this but i can't find any solutions. basically here's the problem i have

In a dns conf file i have something like this

zone "test1.com" {
type slave;
file "slave/db.test1.com";
};

zone "test2.com" {
type slave;
file "slave/db.test2.com";
};

zone "test3.com" {
type slave;
file "slave/db.test3.com";
};


What i need done is to insert the line 'masters { 10.0.0.1; };' below 'file "slave/db.test#.com"; ' keeping }; below it. basically to look like this

zone "test#.com" {
type slave;
file "slave/db.test#.com";
masters { 10.0.0.1; }; #new line
};

I've tried using this command in vi " :%s/};/masters { 10.0.0.1; };}; "
but that leaves me with this

zone "test#.com" {
type slave;
file "slave/db.test#.com";
masters { 10.0.0.1; };};

which i don't like i need the second }; to be below the line "masters"

is there a CR command i can use with this command " :%s/};/masters { 10.0.0.1; };}; " ???

any help or suggestions would be appreciated
thanks
 

oldsaw

Distinguished
Feb 5, 2006
42
0
18,530
The vi text editor is simple to use once you learn its secret - that it was invented by someone who was right handed. Let me explain: When in the "Command Mode" (and I will explain the modes shortly - and soon) your right hand has immediate access to the keyboard's keys "h", "j", "k", "l", and ":". Pressing "h" moves the screen's cursor to the left, and pressing "l" moves it to the right - make sense? Pressing "j" moves the screen's cursor down one line, and pressing "k" moves it up one line. (A coin toss may have solidified that decision.) Pressing ":" puts the entire editor into the "Command Execution Mode", pressing "x" deletes any character then under the cursor (wrong hand but the obvious letter for such a job), and pressing "i" puts the entire editor into the "Insertion Mode". As for the modes there are three: the "Insertion Mode", the "Command Mode", and the "Command Execution Mode". When the vi editor is in the "Insertion Mode" every keyboard stroke produces its corresponding ASCII character on the screen and on (or in) the file which is currently being edited or created, except for the "escape" key: When you are in the "Insertion Mode" pressing the "escape" key moves the entire vi editor into its "Command Mode". While in the "Command Mode" the keyboard's keys do not produce ASCII characters at all; instead, they move the cursor on the screen in the manner I described. While in the "Command Execution Mode" (which you enter from the "Command Mode" by pressing the character ":") you can write your changes and exit the editor by pressing "w" "q" so that the characters ":wq" appear on the bottom left of your screen and then pressing "enter" or you can exit the editor without saving your changes by pressing the characters "q" "!" so that ":q!" appears at the bottom left of your screen and then pressing "enter". Though there are many other vi commands these few will suffice to do your job.
 

linux_0

Splendid
Try this :-D

[code:1:71178329ed]
perl -e 'while(<STDIN>) {$_=~ s/^};/masters { 10.0.0.1; }; \n};/; print "$_"; }' < filename
[/code:1:71178329ed]

It's ugly but it works ;-)




hey fellas new member here :)

been looking all over the web for this but i can't find any solutions. basically here's the problem i have

In a dns conf file i have something like this

zone "test1.com" {
type slave;
file "slave/db.test1.com";
};

zone "test2.com" {
type slave;
file "slave/db.test2.com";
};

zone "test3.com" {
type slave;
file "slave/db.test3.com";
};


What i need done is to insert the line 'masters { 10.0.0.1; };' below 'file "slave/db.test#.com"; ' keeping }; below it. basically to look like this

zone "test#.com" {
type slave;
file "slave/db.test#.com";
masters { 10.0.0.1; }; #new line
};

I've tried using this command in vi " :%s/};/masters { 10.0.0.1; };}; "
but that leaves me with this

zone "test#.com" {
type slave;
file "slave/db.test#.com";
masters { 10.0.0.1; };};

which i don't like i need the second }; to be below the line "masters"

is there a CR command i can use with this command " :%s/};/masters { 10.0.0.1; };}; " ???

any help or suggestions would be appreciated
thanks
 

linux_0

Splendid
In vi

%s/};/masters { 10.0.0.1; };<CTRL+V><ENTER>};

would also have worked

Where <CTRL+V><ENTER> are the corresponding keystrokes CTRL + V + ENTER


There is also a simpler way to do it in perl without using as much code.

:-D
 

linux_0

Splendid
Here's an abbreviated way to do it in perl:


dns.pl:
[code:1:62e4009b27]
$_=~ s/^};/masters { 10.0.0.1; }; \n};/;
[/code:1:62e4009b27]

filename
[code:1:62e4009b27]
zone "test1.com" {
type slave;
file "slave/db.test1.com";
};

zone "test2.com" {
type slave;
file "slave/db.test2.com";
};

zone "test3.com" {
type slave;
file "slave/db.test3.com";
};
[/code:1:62e4009b27]




[code:1:62e4009b27]perl -p dns.pl < filename[/code:1:62e4009b27]



Output
[code:1:62e4009b27]
zone "test1.com" {
type slave;
file "slave/db.test1.com";
masters { 10.0.0.1; };
};

zone "test2.com" {
type slave;
file "slave/db.test2.com";
masters { 10.0.0.1; };
};

zone "test3.com" {
type slave;
file "slave/db.test3.com";
masters { 10.0.0.1; };
};
[/code:1:62e4009b27]


Have fun :-D