简体   繁体   中英

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?

If I decipher your requirements correctly, then

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

should do.

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$ .

使用GNU sed,您可以执行以下命令:

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM