Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

adding blank lines to a file

33 views
Skip to first unread message

Ricardo Pan

unread,
Jun 19, 2007, 3:31:04 PM6/19/07
to
Hello ... how do I add a blank line after some defined number of
lines?

Regards

Example: I have a file with 100 lines, after 10 lines I would like to
add a blank line.

1st line
2nd line
3rd line
4th line
5th line
10th line
blank line
11th line
12th line
...
20th line
blank line
21st line
22nd line
23rd line
...
30th line
blank line
...
...
...
blank line
91st line
92nd line
93rd line
...
100th line

Vassilis

unread,
Jun 19, 2007, 4:01:49 PM6/19/07
to

Ricardo Pan :


awk '{ print } NR % 10 == 0 { print "" }' input

Vassilis

Janis Papanagnou

unread,
Jun 19, 2007, 4:18:24 PM6/19/07
to
Ricardo Pan wrote:
> Hello ... how do I add a blank line after some defined number of
> lines?
>
> Regards
>
> Example: I have a file with 100 lines, after 10 lines I would like to
> add a blank line.

awk 'ORS=NR%10?"\n":"\n\n"'

Janis

meduza

unread,
Feb 8, 2014, 3:52:07 PM2/8/14
to

I think it should be the following:

awk '{print} (NR % 99 == 0) {print ""}'

the problem after the first insertion of empty line with NR % 100 == 0 the nest empty line will be on 201th row which is wrong.

I had a similar problem for awk and your suggestions did not work properly so I played with it and got the right result (every %100th row of my input file has to be blank line).


Jonathan Hankins

unread,
Feb 10, 2014, 4:33:11 PM2/10/14
to
If what you want is for a blank line to be injected into the output stream at every hundredth line, then I don't think this does what you want.

When counting from 1, the hundredth lines are 100, 201, 302, ...

Compare (left column is input line number from nl, right is the actual input from seq):


$ seq 1 1000 | awk 'NR%99==0{print ""}{print}' | nl -ba
1 1
2 2
3 3
...
97 97
98 98
99
100 99
101 100
102 101
103 102
...
197 196
198 197
199
200 198
201 199
202 200
203 201
...
297 295
298 296
299
300 297
301 298
302 299
303 300
...

to:

$ seq 1 1000 | awk 'NR%100==0{print ""}{print}' | nl -ba
1 1
2 2
3 3
...
97 97
98 98
99 99
100
101 100
102 101
103 102
...
197 196
198 197
199 198
200 199
201
202 200
203 201
...
297 295
298 296
299 297
300 298
301 299
302
303 300
...

jonathan...@gmail.com

unread,
Feb 10, 2014, 8:37:43 PM2/10/14
to
On Monday, February 10, 2014 3:33:11 PM UTC-6, Jonathan Hankins wrote:
> On Saturday, February 8, 2014 2:52:07 PM UTC-6, meduza wrote:
>
> > I think it should be the following:
>
> >
>
> >
>
> >
>
> > awk '{print} (NR % 99 == 0) {print ""}'
>
> >
>
> >
>
> >
>
> > the problem after the first insertion of empty line with NR % 100 == 0 the nest empty line will be on 201th row which is wrong.
>
> >
>
> >
>
> >
>
> > I had a similar problem for awk and your suggestions did not work properly so I played with it and got the right result (every %100th row of my input file has to be blank line).
>
>
>
> If what you want is for a blank line to be injected into the output stream at every hundredth line, then I don't think this does what you want.
>
>
>
> When counting from 1, the hundredth lines are 100, 201, 302, ...

This was a braindead statement on my part.

> Compare (left column is input line number from nl, right is the actual input from seq):

I think what you want is"

$ seq 1 1000 | awk '{print}NR%100==0{print ""}' | nl -ba

1 1
2 2
3 3
...
97 97
98 98
99 99
100 100
101
102 101
103 102
104 103
...
198 197
199 198
200 199
201 200
202
203 201
204 202
205 203
...
299 297
300 298
301 299
302 300
303
304 301
305 302
306 303
...
0 new messages