Andy K
Andy K

Reputation: 5075

awk or sed - delete specific lines

I've got this

line1
line2
line3
line4
line5
line6
line7
line8
line9
line10
line11
line12
line13
line14
line15

I want this

line1
line3
line5
line6
line8
line10
line11
line13
line15

As you can see , the line to be deleted are going from x+2 then x+3 , x equals the line number to be deleted.

I tried this with awk but this is not the right way.

awk '(NR)%2||(NR)%3' file > filev1

Any ideas why?

Upvotes: 0

Views: 75

Answers (3)

Adam
Adam

Reputation: 18855

Using GNU sed, you can do the following command:

 sed '2~5d;4~5d' test.txt

Upvotes: 2

Jotne
Jotne

Reputation: 41460

Based on Wintermutes logic :)

awk 'NR%5!~/^[24]$/' file
line1
line3
line5
line6
line8
line10
line11
line13
line15

or

awk 'NR%5~/^[013]$/' file

How it works.

We can see from your lines that the one with * should be removed and other kept.

line1
line2*
line3
line4*
line5

line6
line7*
line8
line9*
line10

line11
line12*
line13
line14*
line15

By grouping data inn to every 5 line NR%5,
We see that line to delete is 2 or 4 in every group. NR%5!~/^[24]$/' This divide data inn to group of 5
Then this part /^[24]$/' tell to not keep 2 or 4
The ^ and $ is important so line 12 47 i deleted too,
since 12 contains 2. So we need to anchor it ^2$ and ^4$.

Upvotes: 2

Wintermute
Wintermute

Reputation: 44063

If I decipher your requirements correctly, then

awk 'NR % 5 != 2 && NR % 5 != 4' file

should do.

Upvotes: 3

Related Questions