[comp.unix.shell] changing a ! to a \nC where \n is a newline

coleman@cam.nist.gov (Sean Sheridan Coleman X5672) (09/18/90)

I am trying to use the global substitution power in vi to 
replace every occurrence of ! with a newline and a C (\nC)

I have tried the following things:

g/;/s//\\nC/g   places the string \\nC where the ; is not a
newline.

g/;/s//\nC/g -- places a nC where the ; is.

Do you know how to make get vi to put a newline not a \n 
when doing the substitution?

How about the reverse, replace a newline with a character?


Sean Coleman
NIST
coleman@bldrdoc.gov

cpcahil@virtech.uucp (Conor P. Cahill) (09/18/90)

In article <5015@alpha.cam.nist.gov> coleman@cam.nist.gov (Sean Sheridan Coleman X5672) writes:
>I am trying to use the global substitution power in vi to 
>replace every occurrence of ! with a newline and a C (\nC)

:1,$s/!/<ctrl>-V<Return>C/g	(this will appear as:1,$s/!/^MC/g)



-- 
Conor P. Cahill            (703)430-9247        Virtual Technologies, Inc.,
uunet!virtech!cpcahil                           46030 Manekin Plaza, Suite 160
                                                Sterling, VA 22170 

norm@oglvee.UUCP (Norman Joseph) (09/18/90)

In <5015@alpha.cam.nist.gov> coleman@cam.nist.gov (Sean Sheridan Coleman X5672) writes:

>I am trying to use the global substitution power in vi to 
>replace every occurrence of ! with a newline and a C (\nC)

Try this:

        :%s/!/^MC/g

Interpretation:  `:' - command mode;  `%' - for all lines in the file;
`s/!/' - substitute for every bang;  `^MC' - a control-M (carriage return)
and the letter C;  `/g' - at every occurence in the line.  Now, here's
the trick -- to enter a literal carriage return (^M) you must precede it
with vi's escape character, ^V.

>How about the reverse, replace a newline with a character?

Use the regular expression meta-charater for end-of-line, $.  Assuming you
want this on all the lines of the file:

        :%s/$/<insert your character here>/    e.g.   :%s/$/!/

Note:  This does not -remove- the newlines from your file; it only allows
you to insert a string -before- the newline.  To actually filter out the
newlines you may have to write your own filter and pipe your file through
it (depending on what you want to do, check the man page for tr(1) first).
The sequence for "straining" your file's contents through a filter is:

        :%!<insert filter command line here>    e.g.    :%!sort -rn

-- 
Norm Joseph - (amanue!oglvee!norm)           cgh!amanue!oglvee!norm@dsi.com, or
  Oglevee Computer Systems, Inc.             ditka!oglvee!norm@daver.bungi.com
                                   ---<*>---
      "Shucking Usenet oysters in pursuit of a pearl."  --  Bill Kennedy

bob@wyse.wyse.com (Bob McGowen x4312 dept208) (09/20/90)

In article <5015@alpha.cam.nist.gov> coleman@cam.nist.gov (Sean Sheridan Coleman X5672) writes:
  >I am trying to use the global substitution power in vi to 
  >replace every occurrence of ! with a newline and a C (\nC)
  >
  >I have tried the following things:
  >
  >g/;/s//\\nC/g   places the string \\nC where the ; is not a
  >newline.
  >
  >g/;/s//\nC/g -- places a nC where the ; is.
  >
  >Do you know how to make get vi to put a newline not a \n 
  >when doing the substitution?

You need to go into the full ex mode first by typing an upper case Q.
Then you would enter:

g/;/s//\
C/g

The backslash at the end of the first line escapes the newline immediately
after so the pattern becomes what you want.

  >
  >How about the reverse, replace a newline with a character?
  >

To globally do this is not something I know how to do.  To do it once or
twice, from the visual mode type J which joins the current line and the
line following into one (this replaces the newline with a space).  Then
you could replace the space with the character you want.  Not too
elegent but it works.  If you have to do it often look up :map for
creating a single key command out of the sequence.

  >
  >Sean Coleman
  >NIST
  >coleman@bldrdoc.gov


Bob McGowan  (standard disclaimer, these are my own ...)
Product Support, Wyse Technology, San Jose, CA
..!uunet!wyse!bob
bob@wyse.com

bob@wyse.wyse.com (Bob McGowen x4312 dept208) (09/20/90)

In article <1990Sep18.023649.1336@virtech.uucp> cpcahil@virtech.UUCP (Conor P. Cahill) writes:
  >In article <5015@alpha.cam.nist.gov> coleman@cam.nist.gov (Sean Sheridan
   Coleman X5672) writes:
    >>I am trying to use the global substitution power in vi to 
    >>replace every occurrence of ! with a newline and a C (\nC)
  >
  >:1,$s/!/<ctrl>-V<Return>C/g	(this will appear as:1,$s/!/^MC/g)
  >

This will put carriage returns in the file, not newlines.

My response to the posters question preceded this.  The procedure is
to go into full ex mode and use the backslash escape to input the
newline:

	g/!/s//\
	C/g

Bob McGowan  (standard disclaimer, these are my own ...)
Product Support, Wyse Technology, San Jose, CA
..!uunet!wyse!bob
bob@wyse.com

dik@cwi.nl (Dik T. Winter) (09/20/90)

In article <2962@wyse.wyse.com> bob@wyse.UUCP (Bob McGowen x4312 dept208) writes:
 > In article <1990Sep18.023649.1336@virtech.uucp> cpcahil@virtech.UUCP (Conor P. Cahill) writes:
 >     >>I am trying to use the global substitution power in vi to 
 >     >>replace every occurrence of ! with a newline and a C (\nC)
 >   >:1,$s/!/<ctrl>-V<Return>C/g	(this will appear as:1,$s/!/^MC/g)
 > This will put carriage returns in the file, not newlines.
Wrong.  It puts newlines (although ^M is carriage return).  Confusing isn't it?
(This is at least true for the versions of vi I have access to.)
--
dik t. winter, cwi, amsterdam, nederland
dik@cwi.nl

gt0178a@prism.gatech.EDU (BURNS,JIM) (09/20/90)

in article <2962@wyse.wyse.com>, bob@wyse.wyse.com (Bob McGowen x4312 dept208) says:
> In article <1990Sep18.023649.1336@virtech.uucp> cpcahil@virtech.UUCP (Conor P. Cahill) writes:
>   >:1,$s/!/<ctrl>-V<Return>C/g	(this will appear as:1,$s/!/^MC/g)
> This will put carriage returns in the file, not newlines.

I thought so too at first, but it works on Dynix and SunOS 4.0.3. Probably
has something to do w/ the equivalent of an 'stty icrnl' mode that vi sets
when executed.
-- 
BURNS,JIM
Georgia Institute of Technology, Box 30178, Atlanta Georgia, 30332
uucp:	  ...!{decvax,hplabs,ncar,purdue,rutgers}!gatech!prism!gt0178a
Internet: gt0178a@prism.gatech.edu

chpetk@gdr.bath.ac.uk (Toby Kelsey) (09/20/90)

>In article <5015@alpha.cam.nist.gov> coleman@cam.nist.gov (Sean Sheridan Coleman X5672) writes:

>  >How about the reverse, replace a newline with a character?

 By doing
  :g/pattern/j
(note small j) in vi, you replace the newline and leading spaces on the 
next line by a space; and
 :g/pattern/j!
concatenates the lines without inserting a space.
-- 
 Toby Kelsey	(chpetk@uk.ac.bath.gdr)

dattier@ddsw1.MCS.COM (David W. Tamkin) (09/21/90)

coleman@cam.nist.gov (Sean Sheridan Coleman) said in <5015@alpha.cam.nist.gov>:

| I am trying to use the global substitution power in vi to 
| replace every occurrence of ! with a newline and a C (\nC)

| Do you know how to make get vi to put a newline not a \n 
| when doing the substitution?

That's been answered twice by now, both people suggesting :%s/!/^V^MC/g.  Of
course, for a single substitution one can just put the cursor onto the ! and
type s<return>C<ESC>.

| How about the reverse, replace a newline with a character?

I can't think of a global way to do that, but JrC (where C is the character)
would do the trick.  Start with the cursor on the line of text whose ending
newline you want to change.  To change the newline to a string,
Js{string}<ESC>.

David Tamkin  Box 7002  Des Plaines IL  60018-7002  708 518 6769  312 693 0591
MCI Mail: 426-1818  GEnie: D.W.TAMKIN  CIS: 73720,1570   dattier@ddsw1.mcs.com

bob@wyse.wyse.com (Bob McGowen x4312 dept208) (09/21/90)

In article <13894@hydra.gatech.EDU> gt0178a@prism.gatech.EDU (BURNS,JIM) writes:
>in article <2962@wyse.wyse.com>, bob@wyse.wyse.com (Bob McGowen x4312 dept208) says:
>> In article <1990Sep18.023649.1336@virtech.uucp> cpcahil@virtech.UUCP (Conor P. Cahill) writes:
>>   >:1,$s/!/<ctrl>-V<Return>C/g	(this will appear as:1,$s/!/^MC/g)
>> This will put carriage returns in the file, not newlines.
>
>I thought so too at first, but it works on Dynix and SunOS 4.0.3. Probably
>has something to do w/ the equivalent of an 'stty icrnl' mode that vi sets
>when executed.

Interesting.  When I get a copy of a DOS format text file (in native format)
and look at it with vi, each line has a "^M" at its end.  My file command
reports this file as a DOS format text file.  If I take a UNIX text file
into vi and do a global substitute:

	%s/$/^-V<CR>/    #  what I see: %s/$/^M/

I get ^M displayed at the end of each line.  Saving this and then running
the file command on it tells me I have a DOS format text file, ie one
with carriage return/linefeed at the end of each line.

If the input ^M were made into a linefeed the file should still be UNIX
only double spaced (in this example).  So under XENIX 2.[23].x and AT&T
UNIX SysV/386 Rel 3.2 there is no conversion of cr to nl set by vi.
Other systems may do this and I would be interested in knowing which ones.

Bob McGowan  (standard disclaimer, these are my own ...)
Product Support, Wyse Technology, San Jose, CA
..!uunet!wyse!bob
bob@wyse.com

cpcahil@virtech.uucp (Conor P. Cahill) (09/21/90)

In article <2962@wyse.wyse.com> bob@wyse.UUCP (Bob McGowen x4312 dept208) writes:
>In article <1990Sep18.023649.1336@virtech.uucp> cpcahil@virtech.UUCP (Conor P. Cahill) writes:
>  >
>  >:1,$s/!/<ctrl>-V<Return>C/g	(this will appear as:1,$s/!/^MC/g)
>  >
>
>This will put carriage returns in the file, not newlines.

Try it.  It will put newlines into the file.


-- 
Conor P. Cahill            (703)430-9247        Virtual Technologies, Inc.,
uunet!virtech!cpcahil                           46030 Manekin Plaza, Suite 160
                                                Sterling, VA 22170 

gt0178a@prism.gatech.EDU (BURNS,JIM) (09/21/90)

in article <2965@wyse.wyse.com>, bob@wyse.wyse.com (Bob McGowen x4312 dept208) says:

> 	%s/$/^-V<CR>/    #  what I see: %s/$/^M/

>                                        So under XENIX 2.[23].x and AT&T
> UNIX SysV/386 Rel 3.2 there is no conversion of cr to nl set by vi.
> Other systems may do this and I would be interested in knowing which ones.

Add A/UX 1.1 and Ultrix 4.0 (new installation) to the list. Your example
produces double spacing on all four systems. (incl. Dynix & SunOS 4.0.3)
-- 
BURNS,JIM
Georgia Institute of Technology, Box 30178, Atlanta Georgia, 30332
uucp:	  ...!{decvax,hplabs,ncar,purdue,rutgers}!gatech!prism!gt0178a
Internet: gt0178a@prism.gatech.edu

cpcahil@virtech.uucp (Conor P. Cahill) (09/21/90)

In article <2965@wyse.wyse.com> bob@wyse.UUCP (Bob McGowen x4312 dept208) writes:
>>> In article <1990Sep18.023649.1336@virtech.uucp> cpcahil@virtech.UUCP (Conor P. Cahill) writes:
>>>   >:1,$s/!/<ctrl>-V<Return>C/g	(this will appear as:1,$s/!/^MC/g)
>>> This will put carriage returns in the file, not newlines.
>
>If the input ^M were made into a linefeed the file should still be UNIX
>only double spaced (in this example).  So under XENIX 2.[23].x and AT&T
>UNIX SysV/386 Rel 3.2 there is no conversion of cr to nl set by vi.
>Other systems may do this and I would be interested in knowing which ones.

As far a I know it works (replaces with a newline) this way on every system. 
Obviously that isn't the case. I have tried it (and know it works that way) 
on Sun OS, HP-UX, Ultrix, and Interactive (both 2.2 and 2.0.2).

I don't have it running anymore to check, but I am pretty sure it
worked this way under microport unix & Bell Tech unix.

-- 
Conor P. Cahill            (703)430-9247        Virtual Technologies, Inc.,
uunet!virtech!cpcahil                           46030 Manekin Plaza, Suite 160
                                                Sterling, VA 22170 

exs@cbnewsk.att.com (eric.sweetman) (09/22/90)

In article <5015@alpha.cam.nist.gov>, Sean Coleman ) writes:
> I am trying to use the global substitution power in vi to 
> replace every occurrence of ! with a newline and a C (\nC)
> ... 
> How about the reverse, replace a newline with a character?

A shell solution to replacing a newline with another character is
to use the tr command which translates a single character into
another.  For example, cat forbar|tr '\012' ' '>tmp replaces
newlines in foobar with spaces and places output in tmp.  012
is the octal code for newline.  Other uses for tr include:
tr '[a-z]' '[A-Z]' translates lower to upper case
tr '()' '{}' translates open(close) parentheses to open(close)
             braces

Eric Sweetman     exs@pruxm.ATT.COM

lee@sq.sq.com (Liam R. E. Quin) (09/22/90)

[this is a vi question, so I've redirected followup thingies to comp.editors]

Sean Sheridan Coleman X5672 (coleman@cam.nist.gov)  writes:
> I am trying to use the global substitution power in vi to 
> replace e;ery occurrence of ! with a newline and a C (\nC)

Vi doesn't understand \n as notation for a newline, but you can
use control-V followed by control-M.

>I have tried the following things:
>g/;/s//\\nC/g   places the string \\nC where the ; is, not a newline.
>g/;/s//\nC/g -- places a nC where the ; is.

Well, these change ; rather than !.  Assuming this is what you mean,
	:g/;/s//^M/g
will do what you want, where ^M is obtained by typing control-V and
then control-M.

If you are in ex instead, (e.g. if you typed ^\ or Q within vi), you
can do
	g/;/s//\
	/g
because ex understands \<newline> to be the same as vi's ^M.

>How about the reverse, replace a newline with a character?

This example will join any line containing "boy" to the one after it,
replacing the newline with the word "whip".

g/boy/s/$/whip/g\
j

You can give a long list of commads after a global, as long as there is a
backslash at the end of each.  (I don't think this works in vi).
So the j will be done on each line matching the pattern, just after the
substitute.

Another common thing to want to do is to get rid of spurious^M
control-M characters at the end of the line (as here),^M
and you can do this by
	:%s/^M$//
where ^M is obtained as above with two keystrokes, control-V control-M.

You cannot include a newline in a pattern, so you can never do
	:%s/boy^Mgirl/girl^Mboy/
for example, to turn
	boy
	girl
into
	girl
	boy
You must use awk or sed, or write a macro, or... well.
Even ex won't let you do that.

Lee

-- 
Liam R. E. Quin,  lee@sq.com, SoftQuad Inc., Toronto, +1 (416) 963-8337
[Granny weatherwax] was opposed to books on strict moral grounds, since she
had heard that many of them were written by dead people  and therefore it
stood to reason reading them would be as bad as necromancy.  [Equal Rites 118]

bob@wyse.wyse.com (Bob McGowen x4312 dept208) (09/22/90)

In article <13958@hydra.gatech.EDU> gt0178a@prism.gatech.EDU (BURNS,JIM) writes:
>in article <2965@wyse.wyse.com>, bob@wyse.wyse.com (Bob McGowen x4312 dept208) says:
>> 	%s/$/^-V<CR>/    #  what I see: %s/$/^M/
...
>> Other systems may do this and I would be interested in knowing which ones.
>
>produces double spacing on all four systems. (incl. Dynix & SunOS 4.0.3)
							     ^^^^^^^^^^^
But we use SunOS 4.0.3 here and when I use the above I DO NOT get double
spacing, just lines with ^M displayed at the end.

Bob McGowan  (standard disclaimer, these are my own ...)
Product Support, Wyse Technology, San Jose, CA
..!uunet!wyse!bob
bob@wyse.com

gt0178a@prism.gatech.EDU (BURNS,JIM) (09/22/90)

in article <2967@wyse.wyse.com>, bob@wyse.wyse.com (Bob McGowen x4312 dept208) says:
> But we use SunOS 4.0.3 here and when I use the above I DO NOT get double
> spacing, just lines with ^M displayed at the end.

Oh well, I do. Here's my stty settings - maybe someone else can figure it
out: (Sun 3/60, rlogin)

speed 19200 baud, 24 rows, 80 columns
parenb -parodd cs7 -cstopb hupcl cread -clocal -crtscts 
-ignbrk brkint ignpar -parmrk -inpck istrip -inlcr -igncr icrnl -iuclc 
ixon ixany ixoff imaxbel 
isig icanon -xcase echo echoe echok -echonl -noflsh -tostop 
echoctl -echoprt echoke 
opost -olcuc onlcr -ocrnl -onocr -onlret -ofill -ofdel 
erase  kill   werase rprnt  flush  lnext  susp   intr   quit   stop   eof
^H     ^X     ^W     ^R     ^O     ^V     ^Z/^Y  ^C     ^\     ^S/^Q  ^D     
-- 
BURNS,JIM
Georgia Institute of Technology, Box 30178, Atlanta Georgia, 30332
uucp:	  ...!{decvax,hplabs,ncar,purdue,rutgers}!gatech!prism!gt0178a
Internet: gt0178a@prism.gatech.edu

dik@cwi.nl (Dik T. Winter) (09/23/90)

In article <2967@wyse.wyse.com> bob@wyse.UUCP (Bob McGowen x4312 dept208) writes:
 > In article <13958@hydra.gatech.EDU> gt0178a@prism.gatech.EDU (BURNS,JIM) writes:
 > >in article <2965@wyse.wyse.com>, bob@wyse.wyse.com (Bob McGowen x4312 dept208) says:
 > >> 	%s/$/^-V<CR>/    #  what I see: %s/$/^M/
 > >> Other systems may do this and I would be interested in knowing which ones.
 > >produces double spacing on all four systems. (incl. Dynix & SunOS 4.0.3)
 > But we use SunOS 4.0.3 here and when I use the above I DO NOT get double
 > spacing, just lines with ^M displayed at the end.
You also said (if I remember right) that you used a Sun 386i.  At least on a
Sun4 (SunOS 4.0.3) it produces double spacing.
--
dik t. winter, cwi, amsterdam, nederland
dik@cwi.nl