0

I would like to copy a couple of screenful lines using vi editor. Anything from line number xxxx to line number zzzzz.

Then, I want to write these lines into another file.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
Sanyifejű
  • 2,610
  • 10
  • 46
  • 73

2 Answers2

2

In the command mode (hit <ESC>) type:

:X,Zy

WhereX is the first line and Z is the last line.

Example

Copy lines 3 to 500:

:3,500y

To insert go to the line after which you want to instert the copies lines and hit p (lower 'P').

If you want to insert the lines befor a particular line then hit P (upper 'P').

Community
  • 1
  • 1
Sebastian Stigler
  • 6,819
  • 2
  • 29
  • 31
1

If you want to do this in vi then you can use:

:XXX,ZZZy<enter>

However, it looks like you want to store these lines in another file. Then, awk comes handy:

awk 'NR==XXX,NR==ZZZ' a > new_file

If the numbers happen to be variables, use them as this:

awk -v first="$first" -v last="$last" 'NR==first,NR==last' a > new_file

Test

Let's print a sequence of 50 numbers in the file a, each one in a different line: $ seq 50 > a

Then, we produce the output:

$ awk 'NR==5,NR==7' a > new_file
$ cat new_file
5
6
7
fedorqui
  • 275,237
  • 103
  • 548
  • 598