[net.unix] Help with sed Syntax

quilico@CECOM-2.arpa (Grace Quilico) (10/21/86)

I'm have been trying (unsuccessfully) to determine what the correct syntax
would be for sed to substitute a given expression with multiple newlines.
For example, someone from an IBM environment ftp'd a file to me (VAX UNIX
System 5.2.2) which is in an IBM printer format.  Except for the first line
in the file, the beginning of each line in this file (it's over 4 megs) has
a leading space, followed by the printer control character (e.g., 1 for
top of page, 0 for double-space, - for triple-space, or a blank for space).
(The first line has a + in column 1, and a 1 in column 2, followed by the
data.)  It seems to me that sed would be the utility to use to convert
these characters.

My problem is with trying to embed newlines in this file.  For example,
I would like to do something like the following:

    s/^ 0/\
    /

where the backslash is to escape a newline character (i.e., substitute
a newline for the ' 0', effectively giving me double-spacing), whenever
the first two characters on a line are ' 0'.  The most I can get it to do 
is either just remove the ' 0' at the beginning of the line, or I manage to 
get an embedded newline in *every* line in the file.  Can someone with a 
little more expertise please help me out?

Thanks in advance,

Grace Quilico
Technical Liaison
<quilico@cecom-2.arpa>
AV 995-2275
Comm: (201) 544-2275

kvamme%vax.runit.unit.uninett@NTA-VAX.arpa (P}l Kvamme) (10/22/86)

> I'm have been trying (unsuccessfully) to determine what the correct syntax
> would be for sed to substitute a given expression with multiple newlines.
> ....
> For example,I would like to do something like the following:
>
>    s/^ 0/\
>    /
>
>where the backslash is to escape a newline character.

Try this:
     /^ 0/i\            # insert text before all occurences of ^ 0
                        # text to insert - a blank line
     s/^ 0//            # get rid of the ^ 0 on the start of the line

Of course, it only works when the expression to substitute is at column 1.
Beware of quoting shell metacharacters. 
It might be best to place the commands on a file, and use sed -f cmds.

P}l Kvamme
Computer science student
NIT, Trondheim, Norway
ARPA:  kvamme@vax.runit.unit.uninett@tor.arpa
VOICE: + 47 7 593674

dce@mips.UUCP (10/22/86)

(Original article asks how to replace '^ 0.*' with a newline and the data.
I would have replied directly but had too much trouble with the address.)

One way is to do something like:

	s/^ 0\(.*\)$/\
	\1

This says 'replace the sequence <space>0<text> with a newline followed by
the text'.

You might also consider using awk for the whole thing. For example, the
following awk script will convert a leading " 1" to an extra newline,
and a leading " 2" to two extra newlines. It could be easily modified
to understand formfeeds and other controls.

	{
		if (substr($0, 1, 1) != " ") {
			print substr($0, 3);
			next;
		}

		Cntl_char = substr($0, 2, 1);

		if (Cntl_char == "1") {
			print "";
		} else if (Cntl_char == "2") {
			print "\n";
		}
		print substr($0, 3);
	}

			David

john@moncol.UUCP (John Ruschmeyer) (10/27/86)

In article <4785@brl-smoke.ARPA> quilico@CECOM-2.arpa (Grace Quilico) writes:
>I'm have been trying (unsuccessfully) to determine what the correct syntax
>would be for sed to substitute a given expression with multiple newlines.
>For example, someone from an IBM environment ftp'd a file to me (VAX UNIX
>System 5.2.2) which is in an IBM printer format.  Except for the first line
>in the file, the beginning of each line in this file (it's over 4 megs) has
>a leading space, followed by the printer control character (e.g., 1 for
>top of page, 0 for double-space, - for triple-space, or a blank for space).
>(The first line has a + in column 1, and a 1 in column 2, followed by the
>data.)  It seems to me that sed would be the utility to use to convert
>these characters.

Hmm.. except for the first line of the file, why not just lose the leading
sapce in each line (something like sed 's/^ //') and run the remainder
through 'asa'.

-- 
Name:		John Ruschmeyer
US Mail:	Monmouth College, W. Long Branch, NJ 07764
Phone:		(201) 571-3451
UUCP:		...!vax135!petsd!moncol!john	...!princeton!moncol!john
						   ...!pesnta!moncol!john

	This isn't a coronation- this is bad comedy.
						-Galvatron