How do I insert a blank line every n lines using awk?

血红的双手。 提交于 2019-12-03 01:34:07

A more "awk-ish" way to write smcameron's answer:

awk -v n=5 '1; NR % n == 0 {print ""}'

The "1;" is a condition that is always true, and will trigger the default action which is to print the current line.

smcameron
awk '{ if ((NR % 5) == 0) printf("\n"); print; }'

for n == 5, of course. Substitute whatever your idea of n is.

Joyer
awk '{print; if (FNR % 5 == 0 ) printf "\n";}' your_file

I guess 'print' should be before 'printf', and FNR is more accurate for your task.

More awkishness:

awk 'ORS=NR%5?RS:RS RS'

For example:

$ printf "%s\n" {1..12} | awk 'ORS=NR%5?RS:RS RS'
1
2
3
4
5

6
7
8
9
10

11
12
$ awk -v n=5 '$0=(!(NR%n))?"\n"$0:$0'

If you want to change 'n', please set the parameter 'n' by awk's -v option.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!