changing a ! to a \nC where \n is a newline

Norman Joseph norm at oglvee.UUCP
Tue Sep 18 23:11:34 AEST 1990


In <5015 at alpha.cam.nist.gov> coleman at 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 at dsi.com, or
  Oglevee Computer Systems, Inc.             ditka!oglvee!norm at daver.bungi.com
                                   ---<*>---
      "Shucking Usenet oysters in pursuit of a pearl."  --  Bill Kennedy



More information about the Comp.unix.shell mailing list