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.
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.
In the command mode (hit <ESC>) type:
:X,Zy
WhereX
is the first line and Z
is the last line.
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').
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
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