[comp.unix.questions] Inserting Blank Line into File

tkevans@oss670.UUCP (Tim Evans) (03/15/91)

I would like to be able to insert blank lines at regular intervals
in a structured file.  Specifically, I want to place a blank line after
every third line in my input file.  Every third line in the input file
begins with a specific character string.

Here is an example input file:

Name: data ...
Addr: data ...
Phone: data ...
Name: data ...
Addr: data ...
Phone: data ...

What I want as output is:

Name: data ...
Addr: data ...
Phone: data ...

Name: data ...
Addr: data ...
Phone: data ...

If you reply via E-Mail, please use the address *BELOW*.

Thanks.
-- 
INTERNET	tkevans%woodb@mimsy.umd.edu
UUCP 		...!{rutgers|ames|uunet}!mimsy!woodb!tkevans
US MAIL		6401 Security Blvd, 2-Q-2 Operations, Baltimore, MD  21235	
PHONE		(301) 965-3286

jik@athena.mit.edu (Jonathan I. Kamens) (03/16/91)

In article <807@oss670.UUCP>, tkevans@oss670.UUCP (Tim Evans) writes:
|> I would like to be able to insert blank lines at regular intervals
|> in a structured file.  Specifically, I want to place a blank line after
|> every third line in my input file.  Every third line in the input file
|> begins with a specific character string.

awk '{print} NR % 3 == 0 {print ""}'

-- 
Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik@Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710

tchrist@convex.COM (Tom Christiansen) (03/16/91)

From the keyboard of tkevans@oss670.UUCP (Tim Evans):
:I would like to be able to insert blank lines at regular intervals
:in a structured file.  Specifically, I want to place a blank line after
:every third line in my input file.  Every third line in the input file
:begins with a specific character string.

I hate doing multi-line things in sed: it's such a pain.

If you cna really do this just by putting an extra newline
after each third line rather than keying off /^Name:/, just
use one of these:

    awk '{ print; if (!(NR % 3)) print "\n" }'

    perl -pe 's/$/\n/ unless $. % 3'

    perl -pe '$. % 3 || s/$/\n/'

--tom

ed@lvw6.lvw.loral.com (Ed Allen) (03/16/91)

Now for the 'sed' solution....

sed -n -e '{N;N;p;a\

}' filename

Should work from 'sh', 'csh' is more particular about multi-line
arguments.
--
Never trust a man who wears white shoes.           | Ed Allen
Vote Libertarian...     Scare the Hell out of 'em. | Loral Command & Contr. Sys.

bhoughto@hopi.intel.com (Blair P. Houghton) (03/16/91)

In article <ED.91Mar15201957@lvw6.lvw.loral.com> ed@lvw6.lvw.loral.com (Ed Allen) writes:
>Now for the 'sed' solution....
>sed -n -e '{N;N;p;a\
>
>}' filename
>Should work from 'sh', 'csh' is more particular about multi-line
>arguments.

Csh(1) version:

sed -n '{N;N;p;a\\
\
}' filename

				--Blair
				  "Picky, picky."

janne@enea.se (Jan Carlsson) (03/17/91)

In article <1991Mar15.190607.5061@convex.com> tchrist@convex.COM (Tom Christiansen) writes:
 >From the keyboard of tkevans@oss670.UUCP (Tim Evans):
 >:I would like to be able to insert blank lines at regular intervals
 >:in a structured file.  Specifically, I want to place a blank line after
 >:every third line in my input file.  Every third line in the input file
 >:begins with a specific character string.
 >
 >I hate doing multi-line things in sed: it's such a pain.
 >
 >If you cna really do this just by putting an extra newline
 >after each third line rather than keying off /^Name:/, just
 >use one of these:
 >
 >    awk '{ print; if (!(NR % 3)) print "\n" }'
				   ^^^^^

Make that "printf" instead since "print" always appends a newline
(ORS actually).
-- 
Jan Carlsson, Enea Data AB, Box 232, Nytorpsvaegen 5, S-183 23 Taeby, Sweden
Phone: +46 8 792 25 00  !  e-mail: janne@enea.se
Fax:   +46 8 768 43 88  !

darcy@druid.uucp (D'Arcy J.M. Cain) (03/18/91)

In article <1991Mar15.190607.5061@convex.com> Tom Christiansen writes:
>From the keyboard of tkevans@oss670.UUCP (Tim Evans):
>:I would like to be able to insert blank lines at regular intervals
>    awk '{ print; if (!(NR % 3)) print "\n" }'

Nope.  Either leave out the newline in the print statement or use printf
instead of print.  The way you have it you get 2 lines instead of one.

-- 
D'Arcy J.M. Cain (darcy@druid)     |
D'Arcy Cain Consulting             |   There's no government
Toronto, Ontario, Canada           |   like no government!
+1 416 424 2871                    |

nt@otter.hpl.hp.com (Nicolas Tripon) (03/19/91)

sed 's/^\(Phone:.*\)$/\1\
/' file
(the first line ends with back slash followed by line feed).

nt@otter.hpl.hp.com (Nicolas Tripon) (03/20/91)

sed '/Phone:/s/$/\
/' file

rbj@uunet.UU.NET (Root Boy Jim) (03/28/91)

>In article <ED.91Mar15201957@lvw6.lvw.loral.com> ed@lvw6.lvw.loral.com (Ed Allen) writes:
>>Now for the 'sed' solution....
>>sed -n -e '{N;N;p;a\
>>
>>}' filename

Most of it anyway. Works if the number of lines is a multiple of three.
Extra line or two gets eaten.

Anyway, here's a single line solution without the same bug:

	sed -n -e '$p;N;$p;N;h;s/.*//;H;x;p'

I think TC is right about avoiding sed in cases like this.
-- 
		[rbj@uunet 1] stty sane
		unknown mode: sane