[comp.unix.wizards] vi and sed questions

rowland@hpavla.HP.COM (Fred Rowland) (09/08/89)

Two questions about editing; one about vi and one about sed!

I've been using these programs for several years and have never
been able to find answers.  I'd appreciate help.

vi question

	There are lots of neat things you can do by mapping a
series of commands to a key (` is my favorite).  If the
command string ends with (Ctrl-V)(Return), you can hold the
key down and load up some command buffer with multiple copies
of the command string.  The effect is that the command marches
down through the file as you watch the action.  But it's slow;
far slower than an addressed or global action.  But it's also
versatile--you can accomplish things that can't be done using
addressed or global stuff.  Is there any way to "load up" a
key with the command string and then execute it via an
addressed or global command?

	Example:

	lastname firstname middlename @address@ ...

	can be changed to

	firstname middlename lastname @address@ ...

	by this key mapping:

	:map ` 0dwf$P(CTRL-V)(Return)

	Just start at line 1, hold ` down, and watch the fun.
	But there ought to be a better way.


sed question

	The file used in the above example is a mail list which
has to be converted into labels.  I add three @ symbols to the end
of each line, then convert all of them to newlines.  I can do this
in vi but I get substitution overflows as the file grows considerably
in size.  I would prefer to use sed, but how do you tell sed to
split a line (generate a newline)?  I've tried various 
combinations of \(Return), \n, CTRL-V, and such but nothing
works.  I EVEN READ THE MANUAL!  It told me to use \n and
gave an example.  It didn't work.  This has to be possible, but
how?


Thanks,
Fred (perplexed) Rowland
Hewlett-Packard Avondale Division

yuf@mentor.cc.purdue.edu (Kyle Grieser) (09/10/89)

In article <4370002@hpavla.HP.COM> rowland@hpavla.HP.COM (Fred Rowland) writes:
>
>vi question
>
>	lastname firstname middlename @address@ ...
>
>	can be changed to
>
>	firstname middlename lastname @address@ ...
>
>	by this key mapping:
>
>	:map ` 0dwf$P(CTRL-V)(Return)
>
>	Just start at line 1, hold ` down, and watch the fun.
>	But there ought to be a better way.

Yeah, there is.  You could just use a global search and replace that
does this for you.  Now, the problem is that these can sometimes be tough
to write for widely varying lines.  I guess you could map it to a key
if you wanted to.  But the following regular expression is a simple example
of how to switch things in a line.  But when you write it, remember that
it always matches the *longest* possible match.  Pretty ugly but it should
work.

	:%s/^\([^ ]*\) \([^ ]*\) \([^ ]*\) \(.*\)$/\2 \3 \1 \4/g

For an explanation of these regular expressions, the ex section of your
Unix manual should do.  But quickly, it means that it will search for the
first 3 words that don't have spaces in them, then anything that follows
them.  Now, the "\(" and "\)" mean that it will remember what it found
so that you can use it in the replacement string.  The first one it finds
will be "\1", the second "\2", etc.  Now you just put them back in the
order you wish.

Hope this helps, and that I didn't forget anything...:-)

-----
Kyle Grieser, Purdue University Computing Center
yuf@mentor.cc.purdue.edu, mentor.cc.purdue.edu!yuf

tr@pcharming.ctt.bellcore.com (tom reingold) (09/11/89)

On the subject of "vi and sed questions", rowland@hpavla.HP.COM (Fred
Rowland) writes:

$ [vi question deleted.  I'll let someone else offer something.]
$ 
$ sed question
$ 
$ 	The file used in the above example is a mail list which
$ has to be converted into labels.  I add three @ symbols to the end
$ of each line, then convert all of them to newlines.  I can do this
$ in vi but I get substitution overflows as the file grows considerably
$ in size.  I would prefer to use sed, but how do you tell sed to
$ split a line (generate a newline)?  I've tried various 
$ combinations of \(Return), \n, CTRL-V, and such but nothing
$ works.  I EVEN READ THE MANUAL!  It told me to use \n and
$ gave an example.  It didn't work.  This has to be possible, but
$ how?

One nice solution is to use tr instead.  Yeah!  I finally get to
promote "my" program -- tr was named after me.  No, just kidding.

I think the command

	tr @ '\012'

should suffice.

Tom Reingold                   |INTERNET:       tr@bellcore.com
Bellcore                       |UUCP:           bellcore!tr
444 Hoes La room 1H217         |PHONE:          (201) 699-7058 [work],
Piscataway, NJ 08854-4182      |                (201) 287-2345 [home]

mouse@mcgill-vision.UUCP (der Mouse) (09/17/89)

In article <4370002@hpavla.HP.COM>, rowland@hpavla.HP.COM (Fred Rowland) writes:
> sed question

> The file used in the above example [ie, the vi question] is a mail
> list which has to be converted into labels.  I add three @ symbols to
> the end of each line, then convert all of them to newlines.  I can do
> this in vi but I get substitution overflows as the file grows
> considerably in size.  I would prefer to use sed,

I don't know how to do that with sed.  But why not just use tr?
"tr @ '\012'" should work fine.

					der Mouse

			old: mcgill-vision!mouse
			new: mouse@larry.mcrcim.mcgill.edu

tarvaine@tukki.jyu.fi (Tapani Tarvainen) (09/18/89)

In article <4370002@hpavla.HP.COM> rowland@hpavla.HP.COM (Fred Rowland) writes:
>Two questions about editing; one about vi and one about sed!
[vi question deleted]

>	The file used in the above example is a mail list which
>has to be converted into labels.  I add three @ symbols to the end
>of each line, then convert all of them to newlines.  I can do this
>in vi but I get substitution overflows as the file grows considerably
>in size.  I would prefer to use sed, but how do you tell sed to
>split a line (generate a newline)?  I've tried various 
>combinations of \(Return), \n, CTRL-V, and such but nothing
>works.  I EVEN READ THE MANUAL!  It told me to use \n and
>gave an example.  It didn't work.  This has to be possible, but
>how?

Strange.  The following 2-line sed script does it just fine for me:

s/@/\
/g

(Nothing invisible in there, just what it looks like.)
Just tried it on four machines (SunOS 4.0, HP-UX 6.5, BSD4.3 in a VAX
and GNUSED in MS-DOS) and it worked just as I expected in them all.
The only problem with this I can think of is that if you're using
csh or tcsh you have to put the script in a file and use the -f option.
What machine, OS and shell are you using?

-- 
Tapani Tarvainen    (tarvaine@tukki.jyu.fi, tarvainen@finjyu.bitnet)

rowland@hpavla.HP.COM (Fred Rowland) (09/20/89)

Thank you, thank you, thank you!  Between notes and email, I now have
the answers I requested.

The reply from Finland, quoted below, gave the crucial clue.

> Strange.  The following 2-line sed script does it just fine for me:
> 
> s/@/\
> /g
> 
> (Nothing invisible in there, just what it looks like.)
> The only problem with this I can think of is that if you're using
> csh or tcsh you have to put the script in a file and use the -f option.
> What machine, OS and shell are you using?

That's exactly what I tried, and I had some typed notes that said that
it worked a few months ago, but it failed now.  Problem:  I switched
from Bourne to csh a while back and didn't know about the need to use
the -f option.

BTW, I'm using hp-UX 6.5 on an HP 330.

Fred Rowland
Avondale Division

merlyn@iwarp.intel.com (Randal Schwartz) (09/23/89)

In article <4370002@hpavla.HP.COM>, rowland@hpavla (Fred Rowland) writes:
| vi question
[...]
| 	Example:
| 	lastname firstname middlename @address@ ...
| 	can be changed to
| 	firstname middlename lastname @address@ ...
| 	by this key mapping:
| 	:map ` 0dwf$P(CTRL-V)(Return)
| 	Just start at line 1, hold ` down, and watch the fun.
| 	But there ought to be a better way.

Entirely within vi:

:g/^\([^ ][^ ]*\)  *\([^ ][^ ]*  *[^ ][^ ]*\)/s//$2 $1/

or cheating with (some versions of) AWK,

:%|awk '{ tmp = $1; $1 = $2; $2 = $3; $3 = tmp; print }'

or cheating with Perl (of course!),

:%|perl -pe 's/^(\w+)\W+(\w+\W+\w+)/$2 $1/;'

Just another hacker,
(but why is this in unix.WIZARDS dudes?)
-- 
/== Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ====\
| on contract to Intel, Hillsboro, Oregon, USA                           |
| merlyn@iwarp.intel.com ...!uunet!iwarp.intel.com!merlyn	         |
\== Cute Quote: "Welcome to Oregon... Home of the California Raisins!" ==/