Issue
Is there any way to delete every m-th and n-th line of a sequence of every K
lines from a file using sed
or awk
?
Example:
cat input.txt
Aline1
Aline2
Aline3
Aline4
Aline5
Aline6
Aline7
Aline8
Aline9
Bline1
Bline2
Bline3
Bline4
Bline5
Bline6
Bline7
Bline8
Bline9
...
I want to remove every 3rd (line3) & 7th (line7) lines of a sequence of every 9 lines of the file. So the output will look like
Aline1
Aline2
Aline4
Aline5
Aline6
Aline8
Aline9
Bline1
Bline2
Bline4
Bline5
Bline6
Bline8
Bline9
...
I tried to combine two conditions at the same time but not successful:
awk '(NR)%3 && (NR)%7' input.txt
Edit:
Here 3rd and 7th lines refer to the lines in each of these sequences (Aline*
,Bline*
...) which consist of 9 lines each.
So the first 9lines of the input file define sequence-A in which I want to remove the 3rd and 7th lines.
The next 9 lines of the input file define sequence B and there I want to do the same. So this would correspond to the 12th and 16th lines of the original file.
PS. I do not want to find by characters*line3
& *line7
and delete them since in general, these lines might contain anything.
Solution
Using GNU sed
$ sed '3~9d;7~9d' input_file
Aline1
Aline2
Aline4
Aline5
Aline6
Aline8
Aline9
Bline1
Bline2
Bline4
Bline5
Bline6
Bline8
Bline9
Answered By - HatLess Answer Checked By - Timothy Miller (WPSolving Admin)