Thread View: comp.unix.shell
11 messages
11 total messages
Started by Sean Sheridan Coleman X5672
Mon, 17 Sep 1990 22:20
changing a ! to a \nC where \n is a newline
Author: Sean Sheridan Coleman X5672
Date: Mon, 17 Sep 1990 22:20
Date: Mon, 17 Sep 1990 22:20
19 lines
833 bytes
833 bytes
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
Re: changing a ! to a \nC where \n is a newline
Author: Conor P. Cahill
Date: Tue, 18 Sep 1990 02:36
Date: Tue, 18 Sep 1990 02:36
12 lines
947 bytes
947 bytes
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
Re: changing a ! to a \nC where \n is a newline
Author: Norman Joseph
Date: Tue, 18 Sep 1990 13:11
Date: Tue, 18 Sep 1990 13:11
35 lines
2014 bytes
2014 bytes
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
Re: changing a ! to a \nC where \n is a newline
Author: Bob McGowen x4312 dept208
Date: Thu, 20 Sep 1990 01:23
Date: Thu, 20 Sep 1990 01:23
44 lines
1939 bytes
1939 bytes
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
Re: changing a ! to a \nC where \n is a newline
Author: Bob McGowen x4312 dept208
Date: Thu, 20 Sep 1990 01:28
Date: Thu, 20 Sep 1990 01:28
22 lines
1309 bytes
1309 bytes
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
Re: changing a ! to a \nC where \n is a newline
Author: Dik T. Winter
Date: Thu, 20 Sep 1990 10:06
Date: Thu, 20 Sep 1990 10:06
11 lines
1136 bytes
1136 bytes
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
Re: changing a ! to a \nC where \n is a newline
Author: Toby Kelsey
Date: Thu, 20 Sep 1990 12:07
Date: Thu, 20 Sep 1990 12:07
12 lines
859 bytes
859 bytes
>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)
Re: changing a ! to a \nC where \n is a newline
Author: David W. Tamkin
Date: Thu, 20 Sep 1990 21:32
Date: Thu, 20 Sep 1990 21:32
21 lines
1514 bytes
1514 bytes
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
Re: changing a ! to a \nC where \n is a newline
Author: Conor P. Cahill
Date: Thu, 20 Sep 1990 23:17
Date: Thu, 20 Sep 1990 23:17
15 lines
1132 bytes
1132 bytes
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
Re: changing a ! to a \nC where \n is a newline
Author: eric.sweetman
Date: Fri, 21 Sep 1990 18:30
Date: Fri, 21 Sep 1990 18:30
16 lines
1253 bytes
1253 bytes
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
Re: changing a ! to a \nC where \n is a newline
Author: Liam R. E. Quin
Date: Sat, 22 Sep 1990 01:12
Date: Sat, 22 Sep 1990 01:12
61 lines
2437 bytes
2437 bytes
[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]
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads