Reputation: 9
This may be a simple question, but I am struggling to find a solution, could anyone provide a simple UNIX sed or awk command to delete every 3rd and 4th line of a file starting from say the 5th line.
Upvotes: 0
Views: 2016
Reputation: 247200
an awk solution, where you pass your numbers as parameters:
awk -v start=5 -v d1=3 -v d2=4 '
{n = NR - start}
n > 0 && (n % d1 == 0 || n % d2 == 0) {next}
1
'
This skips lines 8, 11, 14, ... and 9, 13, 17, ...
Upvotes: 2
Reputation: 882596
Assuming I've understood the requirements, you can use awk
as follows:
pax$ echo '1
2
3
4
5
6
7
8
9
10
11
12
13
14
15' | awk '{if (((NR - 5) % 4 < 2) || (NR < 5)) {print}}'
1
2
3
4
5
6
9
10
13
14
As you can see, this uses NR
(the line number) to decide whether or not to print the line.
Starting from line 5, it prints two then skips two, ad infinitum. Below 5, it prints everything.
If I've misunderstood exactly which lines you want skipped, the theory is still sound, you just have to adjust the if
statement.
Upvotes: 2
Reputation: 46507
How about Perl?
perl -pi -e 'print if $. < 5 or 1 == $. % 4 or 2 = $. % 4' your_file
Upvotes: 0
Reputation: 36422
If you're using GNU sed, you can use the FIRST~STEP
address format, eg.
sed -e '6~3 d' -e '8~4 d' file
deletes the 6th line, then every 3rd, and deletes the 8th line, then every 4th.
Upvotes: 2