1833875 Members
1850 Online
110063 Solutions
New Discussion

Some vi help please ...

 
SOLVED
Go to solution

Some vi help please ...

Let's say I have "originalscript" and want to "yank" lines 1 through 15 and put them in "newscript", here is what you do:

vi +"1,15w! newscript|q" originalscript

This works like a charm, but what if I now wanted to yank some more line from "originalscript" and write them to "newscript"? Thus far it would overwrite newscript, not append like I would want it to. Any ideas my great HP Masterguru Shell Progammers and Perl Magicians? Can I do a merge or something like that?

Thank you, Andy
6 REPLIES 6
Brian M Rawlings
Honored Contributor

Re: Some vi help please ...

Andy: it should be possible to use standard redirect and pipe notation to cause the new yanked lines to append to the existing target file. I haven't tried it, and maybe it wouldn't work as straightforwardly as it seems.

The alternative, of course, is to yank the later lines to a third file, and then simply cat the third file with an "append redirect" (>>) to the target file. If desired, this could all happen in one command line, using semicolons to seperate command sequences.

If you are doing this in a script, however, you could also use variables to hold the yanked lines or the file they went to, and then append them or manipulate them as desired.

As in all UN*X stuff, there are a dozen ways to do anything, six of which are acceptable to most people, and a couple of which are optimal. Maybe somebody else will suggest a more 'optimal' approach...

Best Regards, and good luck with it. --bmr
We must indeed all hang together, or, most assuredly, we shall all hang separately. (Benjamin Franklin)
Rodney Hills
Honored Contributor
Solution

Re: Some vi help please ...

Andy,

With "vi" you can change do

vi +"20,25w >> newscript|q" originalscript

The ">>" will do an "append" write.

A better tool might be sed-

sed -n '1,15p;20,25p'
HTH

-- Rod Hills
There be dragons...
Graham Cameron_1
Honored Contributor

Re: Some vi help please ...

Vi is not really the tool for this. Vi is an interactive, screen-based editor, but you are doing batch type edits.
You should look at sed, or awk, or perl, or ...

My preference is awk. To copy lines 15 through 30 inclusive from file1 and append to file2, something like:

cat file1|awk '(NR >=15 && NR <=30) {print} ' >> file2

- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
H.Merijn Brand (procura
Honored Contributor

Re: Some vi help please ...

perl, I saw perl.

It's about the second perl call, the first is only to generate a list to prove it works:

a5:/u/usr/merijn 102 > perl -le 'print for 1..10'
1
2
3
4
5
6
7
8
9
10
a5:/u/usr/merijn 103 > perl -le 'print for 1..60' | perl -ne'(10..15 or 20..22 or 30..32)and print'
10
11
12
13
14
15
20
21
22
30
31
32
a5:/u/usr/merijn 104 >

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn

Re: Some vi help please ...

Thank you all for answering my question.

Merijin, could you give me an example on how I would use the perl example but reference an actual file to print these lines from. I can duplicate what you are doing, but where in the command would I actually reference my file to get these lines from? I'm very new to perl.

Andy
H.Merijn Brand (procura
Honored Contributor

Re: Some vi help please ...

Funny how mij name sometimes gets misspelled so that I cannot pronounce it myself anymore :) [ No pun intended ] I guess that the wizards are duelling again and expelleriamus is casted to us instead.

a5:/u/usr/merijn 103 > perl -ne'(10..15 or 20..22 or 30..32)and print' your_file

note that since this not so widely known feature uses line ranges, you have to make a single line a range too (e.g. 14..14)

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn