I'd like to use the Vi editor to delete multiple rows in a file. Please give me idea or suggestion.
My goal is like this:
Before:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
.
.
29
30
.
.
After;
1
10
20
30
40
.
.
vivim
I'd like to use the Vi editor to delete multiple rows in a file. Please give me idea or suggestion.
My goal is like this:
Before:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
.
.
29
30
.
.
After;
1
10
20
30
40
.
.
Best Answer
If you mean you want to keep every 10th line and delete the rest:
Explanation:
%
whole filenorm
execute the following commands in "normal mode"9dd
delete 9 linesj
move down one line (i.e. keep it)note: this deletes the first row.
Adapted from http://www.rayninfo.co.uk/vimtips.html
Or using the global command:
:g/^/+ d9
Adapted from https://stackoverflow.com/questions/1946738/vim-how-to-delete-every-second-row
Or you could use
awk
: