Unix Shell - vi break apart multiple email addresses in one line.

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > December 2007 > vi break apart multiple email addresses in one line.





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author vi break apart multiple email addresses in one line.
rich

2007-11-27, 7:21 pm

I have a line in a file that reads like this:

a...@example.com; a...@example.net; a...@example.com

I want to use vi to search for this pattern and put each address on a
new line in the file.

Any ideas?
Janis Papanagnou

2007-11-27, 7:21 pm

rich wrote:
> I have a line in a file that reads like this:
>
> a...@example.com; a...@example.net; a...@example.com
>
> I want to use vi to search for this pattern and put each address on a
> new line in the file.
>
> Any ideas?


Plain vi or vim? In vim you can do :%s/; /^M/g where ^M is typed as
<Ctrl-V> and <Enter>. AFAIR, I couldn't do that in plain vi but I may
be mistaken und you may want to try it. (Ah, and if it's really just
one line than replace the % by a . (dot) while on that line.)

Janis
rich

2007-11-27, 7:21 pm

On Nov 27, 6:41 pm, Janis Papanagnou <Janis_Papanag...@hotmail.com>
wrote:
> rich wrote:
>
>
>
>
> Plain vi or vim? In vim you can do :%s/; /^M/g where ^M is typed as
> <Ctrl-V> and <Enter>. AFAIR, I couldn't do that in plain vi but I may
> be mistaken und you may want to try it. (Ah, and if it's really just
> one line than replace the % by a . (dot) while on that line.)
>
> Janis


plain vi.
Glenn Jackman

2007-11-27, 7:21 pm

At 2007-11-27 06:10PM, "rich" wrote:
> I have a line in a file that reads like this:
>
> a...@example.com; a...@example.net; a...@example.com
>
> I want to use vi to search for this pattern and put each address on a
> new line in the file.


:g/.@./s/;/\r/g

meaning:

"g/.@./" foreach line in the file matching the pattern ".@."
"s/;/\r/g" replace every ";" with a carriage return

--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry
Ed Morton

2007-11-27, 7:21 pm



On 11/27/2007 5:10 PM, rich wrote:
> I have a line in a file that reads like this:
>
> a...@example.com; a...@example.net; a...@example.com
>
> I want to use vi to search for this pattern and put each address on a
> new line in the file.
>
> Any ideas?


We often see people asking to do text mainpulation jobs like this in vi and I've
often wondered "why vi?". It'd be easy in sed or awk or.... so I'm just really
curious about why so often the questions are about how to do it in vi - care to
elucidate?

Ed.

Janis

2007-11-28, 7:36 am

On 28 Nov., 00:51, Ed Morton <mor...@lsupcaemnt.com> wrote:
> On 11/27/2007 5:10 PM, rich wrote:
>
>
>
>
>
> We often see people asking to do text mainpulation jobs like this in
> vi and I've often wondered "why vi?". It'd be easy in sed or awk
> or.... so I'm just really curious about why so often the questions
> are about how to do it in vi - care to elucidate?


I cannot speak for the OP; but I use vi in cases where the task can be
efficiently done with vi(m) and I have just a single file to process.
I experienced some types of replacements to be more bulky in sh/awk
than in vi, and the file handling overhead ("safe mv") is avoidable.
Also if you can't tell in advance where the range to consider for
processing is, or, in other words, if you have to determine the subset
of lines where to operate on by manual inspection anyway.

Janis
rich

2007-11-28, 1:24 pm

On Nov 27, 6:51 pm, Ed Morton <mor...@lsupcaemnt.com> wrote:
> On 11/27/2007 5:10 PM, rich wrote:
>
>
>
>
>
> We often see people asking to do text mainpulation jobs like this in vi and I've
> often wondered "why vi?". It'd be easy in sed or awk or.... so I'm just really
> curious about why so often the questions are about how to do it in vi - care to
> elucidate?
>
> Ed.


For myself it was a project I started in vi and I am using it. I want
to know how to do this in vi. I have used sed before, but I am more
comfortable with vi than sed or awk.
Ed Morton

2007-11-29, 7:34 am



On 11/28/2007 12:22 PM, rich wrote:
> On Nov 27, 6:51 pm, Ed Morton <mor...@lsupcaemnt.com> wrote:
>
>
>
> For myself it was a project I started in vi and I am using it. I want
> to know how to do this in vi. I have used sed before, but I am more
> comfortable with vi than sed or awk.


Thank you very much for responding. I understand where you're coming from - it
is hard to bite the bullet and learn a new tool, especially one that appears
complicated.

Having said that, what you're doing now is a little like if you were building a
house and now wanted to make a window so you were trying to figure out how to do
it using a hammer, since that's the tool you started your project with, rather
than learning how to use a circular saw. Yes, you can make a "window" using a
hammer, but you'll probably be much happier with the result if you learn to use
a circular saw.

To try to help you take a step in the right direction to the more appropriate
tools for general text manipulation, in this case, you got a "vi" (actually
"ed") solution that was:

:g/.@./s/;/\r/g

Assuming that works for you, the sed and awk equivalents would be:

sed '/.@./s/;/\r/g' file
awk '/.@./{ gsub(/;/,"\r"); print }' file

and if you want to have their output replace the contents of the original file,
it's just:

sed/awk '...' file > tmp && mv tmp file

Regards,

Ed.

Geoff Clare

2007-11-29, 1:29 pm

Janis Papanagnou wrote:

> Plain vi or vim? In vim you can do :%s/; /^M/g where ^M is typed as
> <Ctrl-V> and <Enter>. AFAIR, I couldn't do that in plain vi but I may
> be mistaken und you may want to try it.


Yes, you can do that in plain vi. (The special treatment of ^M here is
specified by POSIX.)

--
Geoff Clare <netnews@gclare.org.uk>
me at

2007-12-02, 1:29 pm

| To try to help you take a step in the right direction to the more appropriate
| tools for general text manipulation, in this case, you got a "vi" (actually
| "ed") solution that was:
|
| :g/.@./s/;/\r/g
|
| Assuming that works for you, the sed and awk equivalents would be:
|
| sed '/.@./s/;/\r/g' file
| awk '/.@./{ gsub(/;/,"\r"); print }' file
|


Hi,

sed -f $HOME/bin/email.sed email

s/,/\r/g

The output just ends up with the , replaced with an r
I tried it with a a ctrl-V ctrl-M in the email.sed file
and that really messed things up.

--
Vic




Mark Hobley

2007-12-02, 1:29 pm

me at <my.address@is.invalid> wrote:

> sed -f $HOME/bin/email.sed email


or just use ed (That's sed without the s):

ed email

Mark.

--
Mark Hobley
393 Quinton Road West
QUINTON
Birmingham
B32 1QE

Email: markhobley at hotpop dot donottypethisbit com

http://markhobley.yi.org/

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com