I want to use sed to print n lines, skip n lines, print n lines etc until the end of a text file, starting at a certain line. eg stating at line 4, print 5-9, the skip 10-14, print 15-19 etc
From the file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
I want
5
6
7
8
9
15
16
17
18
19
25
26
27
28
29
35
36
37
38
39
etc
If I try
sed -n '4~5p' FILE.txt > NEWFILE.txt
will give me
4
9
14
19
which I don't want.
I don't think there's an easy way to do this in sed
, as it can't do arithmetic. awk
is better:
awk 'NR%10 >= 5' FILE.txt > NEWFILE.txt
NR%10
is the record number modulo 10 (ie the last digit of the line number). So this prints any line where the last digit of the line number is at least 5: 5-9, 15-19, 25-29, etc.
sed is for simple subsitutions on individual lines, that is all. Just use awk:
$ awk '!(NR%5){f=!f} f' file
5
6
7
8
9
15
16
17
18
19
Here's a sed solution. Try figuring that out ;)
sed -n 'n ; n ; n ; n ; n ; h ; n ; H ; n ; H ; n ; H ; n ; H ; x ; p' file
This might work for you (GNU sed):
sed -n '5~10,+4p' file
Use a range where the first address steps ever 10 lines from line 5 and the second address is 4 lines following the first address. See here for details.
BTW sed -n '4~5p'
does not give the answer you thought.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.