[comp.unix.questions] Invisible Ascii with VI

andy@unx1.UUCP (Andy Clews) (09/23/87)

Does anyone out there know if it's possible to insert/search/replace
non-printing ascii characters?
Specifically: I sometimes get files from a net source that have an "extra"
CR tacked on the end of the line, and VI shows them as ^M on the screen.
For various reasons these are a pain. I'd like to be able to get rid of
them with, say, a global substitute. I've tried 1,$s/^V^M$// but this
does not seem to do what I want.
I'd appreciate email replies if any.

Andy Clews
andy@uk.ac.sussex.unx1

bhj@bhjat.UUCP (09/27/87)

In article <258@unx1.UUCP>, andy@unx1.UUCP (Andy Clews) writes:
> Specifically: I sometimes get files from a net source that have an "extra"
> CR tacked on the end of the line, and VI shows them as ^M on the screen.

Try using "tr" from the shell... works for me...

Burt Janz
BHJ Associates
..decvax!bhjat!bhj

kathy@bakerst.UUCP (09/27/87)

In article <258@unx1.UUCP> andy@unx1.UUCP (Andy Clews) writes:
>Does anyone out there know if it's possible to insert/search/replace
>non-printing ascii characters?

I don't have a global answer to this question, but I know you
can do some things for sure - just remember to precede the
character with a backslash so that it's taken literally.
For example, lots of times, I'll want to remove overstrike
characters and backspaces from nroff output:

	g/.\^H/s///g

This will *look* like

	g/.^H/s///g

when you've finished typing it in, but you actually enter a
backslash and a backspace. 

I'm pretty sure I've done that with ESC characters, too - to
remove the half-linefeed codes.  Wouldn't swear by that, tho.


>Specifically: I sometimes get files from a net source that have an "extra"
>CR tacked on the end of the line, and VI shows them as ^M on the screen.
>For various reasons these are a pain. I'd like to be able to get rid of
>them with, say, a global substitute. I've tried 1,$s/^V^M$// but this
>does not seem to do what I want.

THIS, tho, I do all the time.  Don't bother to try to match the ^M.
Just match a single character at the end of every line - because,
after all, there *is* one at the end of every line:

	1,$s/.$//



Kathy Vincent ------> Home: {ihnp4|mtune|codas|ptsfa}!bakerst!kathy
              ------> AT&T: {ihnp4|mtune|burl}!wrcola!kathy

root@ozdaltx.UUCP (09/27/87)

In article <258@unx1.UUCP>, andy@unx1.UUCP (Andy Clews) writes:
> Does anyone out there know if it's possible to insert/search/replace
> non-printing ascii characters?
> CR tacked on the end of the line, and VI shows them as ^M on the screen.
> them with, say, a global substitute. I've tried 1,$s/^V^M$// but this
> does not seem to do what I want.

I get the same especially since I have a lot of PC users
here. So, not only do I get the ^M, but ^Z's as well. :-)
^M will be the last character on a line, so with vi you can
use;
	1,$s/.$//
or
	:g/^V^M/s///

That's assuming you are actually typing CTRL-V,CTRL-M...

Use the :l (el) option in ed or vi to show the invisable
values in the file.

If you use ed, you should be able to use CTRL-M within the
subsitiute statement, g/CTRL-M/s///. Where CTRL-M is your
pressing CTRL key and M at the same time.

"Works for me...." - some TV cop.

-- 
============================================================
| Scotty                     |  Adapt - Enjoy - Survive    |
| ihnp4!killer!ozdaltx!sysop |  "Ad Venerem Securiorem"    |
============================================================

paul@devon.UUCP (09/28/87)

In article <258@unx1.UUCP> andy@unx1.UUCP (Andy Clews) writes:
> Specifically: I sometimes get files from a net source that have an "extra"
> CR tacked on the end of the line, and VI shows them as ^M on the screen.
> For various reasons these are a pain. I'd like to be able to get rid of
> them with, say, a global substitute. I've tried 1,$s/^V^M$// but this
> does not seem to do what I want.
> I'd appreciate email replies if any.

I thought this reply might be of general interest, so I'm posting it.

Your "1,$s/^V^M$//" didn't work because the search pattern didn't match
anything in your text file.  You asked vi to find all lines where the
character just before the end of line ($) was a carriage-return.  This
pattern will not match because the lines in your text were terminated
by a CR/LF pair, so there was a line-feed between the "^M" and the "$".

A better way to do this would have been "1,$s/^V^M//".  This would replace
all occurrances of ^M with nothing.  You don't need an end-of-line indicator
as there are (should be) no other ^M's in the text.

- paul

-- 
Paul Sutcliffe, Jr.

UUCP (smart):  paul@devon.UUCP
UUCP (dumb):   ...{rutgers,ihnp4,cbosgd}!bpa!vu-vlsi!devon!paul

chris@mimsy.UUCP (Chris Torek) (09/28/87)

In article <469@devon.UUCP> paul@devon.UUCP (Paul Sutcliffe Jr.) writes:
>Your "1,$s/^V^M$//" didn't work because the search pattern didn't match
>anything in your text file.  You asked vi to find all lines where the
>character just before the end of line ($) was a carriage-return.  This
>pattern will not match because the lines in your text were terminated
>by a CR/LF pair, so there was a line-feed between the "^M" and the "$".

Nope.

A linefeed is a Unix newline; CR-LF pairs form a literal control-M
followed by a newline.  vi breaks a file apart at newlines (so as
to create a series of lines), and, internally, does not carry around
the newlines (linefeeds) at all.  There is no line-feed between the
^M and the $-end-of-line, and in fact, typing the characters

	: % s / ^V ^M $ / /

works fine for me.

Of course, this could be a bug in other versions of vi.  (At this
very moment, I am running `Version 3.7, 6/7/85'---so says `:ver'.)

(Going the *other* way---adding ^Ms---is another matter entirely.)
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris

was@hplsdlw.HP.COM (William A. Stubblebine) (09/29/87)

> / hplsdlw:comp.unix.questions / andy@unx1.UUCP (Andy Clews):
> Does anyone out there know if it's possible to insert/search/replace
> non-printing ascii characters?
> Specifically: I sometimes get files from a net source that have an "extra"
> CR tacked on the end of the line ...

Vi will insert non-printing characters (e.g., ^M) using the ^V escape.
That is, if you input ^V^M in insert mode, ^M goes into the buffer.
(Everywhere, that is, except the end of a line, where :s/$/^V^M/ does
work).

Vi will also accept escaped ^M characters as search patterns, e.g.

    :s/^V^M$//g

should remove your trailing ^M's.  (I've uploaded a few XMODEM files
myself!)

Unfortunately, many versions if ex/vi have a bug in handling ^M as a
replacement pattern via ex while in visual mode.  That is, typing

    :s/;/\^M/g

to change 

    hello;goodbye

into

    hello
    goodbye

while in visual mode doesn't work.  The ^M isn't interpreted as the
replacement pattern, it just terminates the command.

However, if you drop into ex mode (type 'Q'), then type

    s/;/\
    /g

Then type 'vi' to return to visual mode you should get what you want.
That is, you leave vi, do your substitute in ex and return as follows:

    Qs/;/\<RET>
    /g<RET>
    vi<RET>

Good luck.
                                Bill Stubblebine
                                Hewlett-Packard Logic Systems Div.
				8245 N. Union Blvd.
                                Colorado Springs, Co. 80918
                                ...!ihnp4!hpfcla!hpldola!was
                                (303) 590-5568

keithe@tekgvs.UUCP (09/29/87)

In article <469@devon.UUCP> paul@devon.UUCP (Paul Sutcliffe Jr.) writes:
<In article <258@unx1.UUCP> andy@unx1.UUCP (Andy Clews) writes:
<> I'd like to be able to get rid of [^M at tend of line]
<> with, say, a global substitute.
<
<I thought this reply might be of general interest, so I'm posting it.
<
<Your "1,$s/^V^M$//" didn't work because the search pattern didn't match
<anything in your text file.  You asked vi to find all lines where the
<character just before the end of line ($) was a carriage-return.  This
<pattern will not match because the lines in your text were terminated
<by a CR/LF pair, so there was a line-feed between the "^M" and the "$".
<

Yet my technique of eliminating these pesky ^M chreatures is

	%s/.$//

to eliminate the last character of the line, on all lines. This, by
your explanation, shouldn't work, but in fact does work. What gives?

keith

irf@kuling.UUCP (Stellan Bergman) (10/01/87)

In article <258@unx1.UUCP> andy@unx1.UUCP (Andy Clews) writes:
>Does anyone out there know if it's possible to insert/search/replace
>non-printing ascii characters?
> .....
>

I think what you need may be to :set beautify in vi or do :Q to take
you into ex mode proper where you can manipulate ^M's and the like.

Otherwise, to insert a control character (^X, say) first hit ^V (<CTRL>-V)
and the next conrol character (in this case: '^X') will not be interpreted but
rather added to the buffer.


Hope this is what you were looking for.

Bo Thide, Swedish Institute of Space Physics. UUCP: ...enea!kuling!irfu!bt

schaefer@ogcvax.UUCP (Barton E. Schaefer) (10/02/87)

In article <hplsdlw.630001> was@hplsdlw.HP.COM (William A. Stubblebine) writes:
}> / hplsdlw:comp.unix.questions / andy@unx1.UUCP (Andy Clews):
}> Does anyone out there know if it's possible to insert/search/replace
}> non-printing ascii characters?
}> Specifically: I sometimes get files from a net source that have an "extra"
}> CR tacked on the end of the line ...
}[ Some explanation of how to do this deleted ]
}
}Unfortunately, many versions if ex/vi have a bug in handling ^M as a
}replacement pattern via ex while in visual mode.  That is, typing
}    :s/;/\^M/g
}to change 
}    hello;goodbye
}into
}    hello
}    goodbye
}while in visual mode doesn't work.  The ^M isn't interpreted as the
}replacement pattern, it just terminates the command.

The way to get around problems like this is to use double ^V characters; that
is, you type

	:s/;/^V^V^V^M/g^M

(you use the RETURN key instead of CTRL-M) which echoes as

	:s/;/^V^M/g

The first ^V assures that the second ^V will be passed on to the ex substitute
command after vi-interface parsing; the third ^V escapes the ^M so it isn't
interpreted as the end of the command.  The ex substitute command then sees
the ^V^M sequence and interprets it as a literal ^M.

I don't know if this works on versions that interpret a backslash-escaped ^M
"correctly", but it works for me (UNIX 4.3 BSD).
-- 
Bart Schaefer			CSNET:	schaefer@Oregon-Grad
				UUCP:	...{tektronix,verdix}!ogcvax!schaefer
"This area is like heaven, and they want to turn it into California."
		-- Joseph Cook, Amish farmer, protesting highway construction

root@hobbes.UUCP (John Plocher) (10/05/87)

+---- chris@mimsy.UUCP (Chris Torek) writes in <8807@mimsy.UUCP> ----
<< a problem with removing trailing ^M's from lines in vi >>
| 	: % s / ^V ^M $ / /
| 
| works fine for me.
| 
| Of course, this could be a bug in other versions of vi.  (At this
| very moment, I am running `Version 3.7, 6/7/85'---so says `:ver'.)

The "vi" distributed by Microport is `Version 3.9, 2/9/83'.  Strange,
when was the timewarp?  (ps, the pattern above works ok, as well as
the one without the trailing '$')

| (Going the *other* way---adding ^Ms---is another matter entirely.)

Yup :-)

| In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690)


-- 
John Plocher uwvax!geowhiz!uwspan!plocher  plocher%uwspan.UUCP@uwvax.CS.WISC.EDU

geoff@ncr-sd.SanDiego.NCR.COM (Geoffrey R. Walton) (10/05/87)

In article <250@hobbes.UUCP> root@hobbes.UUCP (John Plocher) writes:
>+---- chris@mimsy.UUCP (Chris Torek) writes in <8807@mimsy.UUCP> ----
><< a problem with removing trailing ^M's from lines in vi >>
>| 	: % s / ^V ^M $ / /
>| 
>| works fine for me.
>| 
>| (Going the *other* way---adding ^Ms---is another matter entirely.)
>
>Yup :-)
>

What's wrong with:

:g/$/s//

This, of course, supposes that you want to insert a blank line
between each line of existing text.  (That isn't what you want
to do?  OK, I give up.  Why _DO_ you want to add "hard" carriage
returns to the lines in your file?)

#include <all_usual_disclaimers.h>

Geoff Walton
Software Publications
NCR E&M San Diego
geoff.walton@SanDiego.NCR.COM
or
{sdcsvax,cbosgd,pyramid,nosc.ARPA,ihnp4}!ncr-sd!geoff
Even the smallest problem becomes unsolvable if enough
meetings are held to discuss it.

avr@hou2d.UUCP (Adam V. Reed) (10/06/87)

In article <250@hobbes.UUCP>, root@hobbes.UUCP (John Plocher) writes:
> +---- chris@mimsy.UUCP (Chris Torek) writes in <8807@mimsy.UUCP> ----
> << a problem with removing trailing ^M's from lines in vi >>
> | 	: % s / ^V ^M $ / /
> | works fine for me.
> | Of course, this could be a bug in other versions of vi.  (At this
> | very moment, I am running `Version 3.7, 6/7/85'---so says `:ver'.)
> The "vi" distributed by Microport is `Version 3.9, 2/9/83'.  Strange,
> when was the timewarp?  (ps, the pattern above works ok, as well as
> the one without the trailing '$')
> | (Going the *other* way---adding ^Ms---is another matter entirely.)
> Yup :-)
> | In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690)
> John Plocher uwvax!geowhiz!uwspan!plocher  plocher%uwspan.UUCP@uwvax.CS.WISC.EDU

Not really. Try :1,$s/$/\^V^M/ which will echo as
:1,$s/$/\^M/
and works as advertized, at least for me (I use System V).
				Adam Reed (hou2d!avr)

alen@cogen.UUCP (Alen Shapiro) (10/06/87)

In article <1805@ncr-sd.SanDiego.NCR.COM> geoff@ncr-sd.SanDiego.NCR.COM (Geoffrey R. Walton) writes:
>In article <250@hobbes.UUCP> root@hobbes.UUCP (John Plocher) writes:
>>+---- chris@mimsy.UUCP (Chris Torek) writes in <8807@mimsy.UUCP> ----
>><< a problem with removing trailing ^M's from lines in vi >>
>>| 	: % s / ^V ^M $ / /
>>| works fine for me.
>>| (Going the *other* way---adding ^Ms---is another matter entirely.)
>What's wrong with:
>:g/$/s//
>This, of course, supposes that you want to insert a blank line
>between each line of existing text.  (That isn't what you want
>to do?  OK, I give up.  Why _DO_ you want to add "hard" carriage
>returns to the lines in your file?)

if you REALLY want to do this in vi - try this (it works on the sun)
and has the added advantage of not adding ^Ms to lines already ending in
one.

vi your favourite file - (/etc/termcap will do)
do the following;
/[^ ^V ^M $]/<CR>	# no spaces in the real thing though
A ^V ^M <ESC>		# again no spaces - this should add a ^M to a line
" m y <space>		# put the ^M int reg 'M' (one real space on this line)
:map v n"mpV<CR>	# def macro v to find line with no ^M at end and add one
:map V v<CR>		# recurse till 'n' fails

type V or v to vi and watch the fun before your very eyes. Of course a
5 line C program would be quicker but who needs efficiency when you can
watch vi do its stuff.

The two tricks 1) using a register for the ^M and 2) using a preset search
strings are necessary on the sun cos a ^M in a macro is taken to act as a
<NL> and square brackets in a search string within a macro confused our
version of vi.

enjoy

--alen the Lisa slayer (it's a long story)
	...seismo!esosun!cogen!alen

ps one good reason for wanting to perform this function is to prepare
a text file for an os which demands <CR><LF> at the end of each line.

PAAAAAR%CALSTATE.BITNET@WISCVM.WISC.EDU (10/07/87)

Keywords: vi, wordstar, wordwrapp
Here is a useful 2cents worth:
If you have an uploaded WORDSTAR file a quick %s/ $// will reduce its length.
A wordstar file is wordwrapped by adding a CR/LF *after* the last space that
fits on the line.
You can also use this to 'unwrap' a file quickly.

Is this format a standard? Shouldn't it be a standard?
Dick Botting
paaaaar@calstate.bitnet
PAAAAAR%CALSTATE.BITNET@WISCVM.WISC.EDU
Comp Sci, CSUSB, 5500 State Univ Pkwy, San Bernardino, CA 92407
(714)887-7368(voice), (714)887-7365(modem)
Disclaimer: I am only an egg

allbery@ncoast.UUCP (Brandon Allbery) (10/10/87)

As quoted from <250@hobbes.UUCP> by root@hobbes.UUCP (John Plocher):
+---------------
| +---- chris@mimsy.UUCP (Chris Torek) writes in <8807@mimsy.UUCP> ----
| | Of course, this could be a bug in other versions of vi.  (At this
| | very moment, I am running `Version 3.7, 6/7/85'---so says `:ver'.)
| 
| The "vi" distributed by Microport is `Version 3.9, 2/9/83'.  Strange,
| when was the timewarp?  (ps, the pattern above works ok, as well as
+---------------

No timewarp; Version 3.7 is the latest termcap-based version (at least, for
non-Berkeley sites), Version 3.9 is the latest terminfo version.  The two
develop independently of each other, so comparing version numbers and dates
doesn't work.
-- 
	    Brandon S. Allbery, moderator of comp.sources.misc
  {{harvard,mit-eddie}!necntc,well!hoptoad,sun!mandrill!hal}!ncoast!allbery
ARPA: necntc!ncoast!allbery@harvard.harvard.edu  Fido: 157/502  MCI: BALLBERY
   <<ncoast Public Access UNIX: +1 216 781 6201 24hrs. 300/1200/2400 baud>>
	 "...he calls _that_ a `little adventure'?!"  - Cmdr. Ryker

PAAAAAR%CALSTATE.BITNET@wiscvm.wisc.EDU (10/19/87)

Keywords: vi, wordstar, wordwrapp
Here is a useful 2cents worth:
If you have an uploaded WORDSTAR file a quick %s/ $// will reduce its length.
A wordstar file is wordwrapped by adding a CR/LF *after* the last space that
fits on the line.
You can also use this to 'unwrap' a file quickly.

Is this format a standard? Shouldn't it be a standard?
Dick Botting
paaaaar@calstate.bitnet
PAAAAAR%CALSTATE.BITNET@WISCVM.WISC.EDU
Comp Sci, CSUSB, 5500 State Univ Pkwy, San Bernardino, CA 92407
(714)887-7368(voice), (714)887-7365(modem)
Disclaimer: I am only an egg

PS - Second Attempt - first bounced back from somewhere in UUCP!? - doc-dick