我想使用awk删除从第27行开始的每第n行

时间:2019-04-12 13:26:09

标签: awk

我有一个大文本文件,它的标题有27行。我想将标头保留在新文件中,但是在数据开始的第28行,我只想保留第10行。

因此,我的新文件看起来将与原始文件一样,其中将包含27行标题,但是新文件中不会包含所有的第十行数据。

我一直在尝试使用“ awk”,我也尝试过“ sed”,我可以让它每隔第10行给我,但是我不能让它包含标题行,即从第28行开始,然后给出每10行

2 个答案:

答案 0 :(得分:0)

请您尝试以下。

awk 'FNR<=27{print;next} ++count%10==0{print;count=""}'  Input_file

或者,如果您想使其更通用,因此在此处添加变量,可以在此处更改变量值,并根据需要获得结果。

awk -v lines_till_print="27"  -v nth_line_print="10" '
FNR<=lines_till_print{
  print
  next
}
++count%nth_line_print==0{
  print
  count=""
}
'  Input_file

答案 1 :(得分:0)

要在5行标题后每3行打印一次:

game 1 started
game 1 turn #1
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #2
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #3
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #4
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #5
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #6
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #7
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #8
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #9
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #10
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #11
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #12
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #13
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #14
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #15
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #16
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #17
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #18
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #19
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #20
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #21
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #22
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #23
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #24
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #25
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #26
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #27
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #28
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #29
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 turn #30
game 1 player judit begins 5 second move
game 1 player judit ends 5 second move
game 1 player opponent 1 begins 55 second move
game 1 player opponent 1 ends 55 second move
game 1 ended
game 2 started
game 2 turn #1
game 2 player judit begins 5 second move
game 2 player judit ends 5 second move
game 2 player opponent 2 begins 55 second move
game 2 player opponent 2 ends 55 second move
game 2 turn #2
game 2 player judit begins 5 second move
game 2 player judit ends 5 second move
game 2 player opponent 2 begins 55 second move
game 2 player opponent 2 ends 55 second move
game 2 turn #3
game 2 player judit begins 5 second move
[...]

或者如果要打印5行标题后的第一行:

$ seq 20 | awk -v h=5 -v r=3 'NR<=h || !((NR-h)%r)'
1
2
3
4
5
8
11
14
17
20

或者,如果您愿意:

$ seq 20 | awk -v h=6 -v r=3 'NR<=h || !((NR-h)%r)'
1
2
3
4
5
6
9
12
15
18

更改变量值以适合。