substitution, transform string to list?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

substitution, transform string to list?

trialero
Hi,

I encountered a nice question to transform some data, and are puzzled
how to do this in Vim. Preferably with the least possible keystrokes,
substitution above a macro. The complete set to transform is some 80k
lines and the first word might differ per line.

Who can help here?

The data to transform:

ABBEEL;1872/253;1882/576;1886/1925;1887/603;1887/1813;1894/1444;1898/3449
ABBEELS;1888/1401;1889/562;1891/2371;1899/3088;1902/732;1903/1509;1904/1317

should become:

ABBEEL;1872/253
ABBEEL;1882/576
ABBEEL;1886/1925
ABBEEL;1887/603
ABBEEL;1887/1813
ABBEEL;1894/1444
ABBEEL;1898/3449
ABBEELS;1888/1401
ABBEELS;1889/562
ABBEELS;1891/2371
ABBEELS;1899/3088
ABBEELS;1902/732
ABBEELS;1903/1509
ABBEELS;1904/1317

Regular substitution `:s/;/\rABBEEL;/g' only works for each line and the
first word has to be typed by hand. I tried using wildcards like `\w\+'
and `.*' but only got a messy result (source is here:
https://stackoverflow.com/questions/10336609/is-it-possible-to-use-find-and-replace-on-a-wildcard-string-in-vim)

TIA,

//meine

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20200717091014.GA999%40trackstand.
Reply | Threaded
Open this post in threaded view
|

Re: substitution, transform string to list?

Chris Willis
Hi,

I encountered a nice question to transform some data, and are puzzled
how to do this in Vim. Preferably with the least possible keystrokes,
substitution above a macro. The complete set to transform is some 80k
lines and the first word might differ per line.

Who can help here?

The data to transform:

ABBEEL;1872/253;1882/576;1886/1925;1887/603;1887/1813;1894/1444;1898/3449
ABBEELS;1888/1401;1889/562;1891/2371;1899/3088;1902/732;1903/1509;1904/1317

should become:

ABBEEL;1872/253
ABBEEL;1882/576
ABBEEL;1886/1925
ABBEEL;1887/603
ABBEEL;1887/1813
ABBEEL;1894/1444
ABBEEL;1898/3449
ABBEELS;1888/1401
ABBEELS;1889/562
ABBEELS;1891/2371
ABBEELS;1899/3088
ABBEELS;1902/732
ABBEELS;1903/1509
ABBEELS;1904/1317

Regular substitution `:s/;/\rABBEEL;/g' only works for each line and the
first word has to be typed by hand. I tried using wildcards like `\w\+'
and `.*' but only got a messy result (source is here:
https://stackoverflow.com/questions/10336609/is-it-possible-to-use-find-and-replace-on-a-wildcard-string-in-vim)

TIA,

//meine

How about a perl one-liner:

    perl -F; -nae "$a = shift(@F) ; for (@F){ chomp; print(qq($a;$_\n));}"
vtd.txt

where vtd.txt is your data file and you can redirect the output somewhere
other than the current STDOUT.

Chris Willis

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/3F7609281FC64FD98CC283EC149E7D6A%40ChrisPCW7.
Reply | Threaded
Open this post in threaded view
|

Re: substitution, transform string to list?

Tim Chase
In reply to this post by trialero
On 2020-07-17 11:10, meine wrote:

> The data to transform:
>
> ABBEEL;1872/253;1882/576;1886/1925;1887/603;1887/1813;1894/1444;1898/3449
> ABBEELS;1888/1401;1889/562;1891/2371;1899/3088;1902/732;1903/1509;1904/1317
>
> should become:
>
> ABBEEL;1872/253
> ABBEEL;1882/576
> ABBEEL;1886/1925
> ABBEEL;1887/603
> ABBEEL;1887/1813
> ABBEEL;1894/1444
> ABBEEL;1898/3449
> ABBEELS;1888/1401
> ABBEELS;1889/562
> ABBEELS;1891/2371
> ABBEELS;1899/3088
> ABBEELS;1902/732
> ABBEELS;1903/1509
> ABBEELS;1904/1317

You can do it in two passes, one to prefix each item on its own line:

  :%s/\%(^\([^;]*\).*\)\@<=\(;[^;]*\)/\r\1\2/g

and then a second command to delete all the remnant lines (ones that
are just the prefix which don't have a ";" in them):

  :v/;/d

-tim



--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20200717074606.11a74992%40bigbox.attlocal.net.
Reply | Threaded
Open this post in threaded view
|

Re: substitution, transform string to list?

trialero
On Fri, Jul 17, 2020 at 07:46:06AM -0500, Tim Chase wrote:

> On 2020-07-17 11:10, meine wrote:
> > The data to transform:
> >
> > ABBEEL;1872/253;1882/576;1886/1925;1887/603;1887/1813;1894/1444;1898/3449
> > ABBEELS;1888/1401;1889/562;1891/2371;1899/3088;1902/732;1903/1509;1904/1317
> >
> > should become:
> >
> > ABBEEL;1872/253
> > ABBEEL;1882/576
> > ABBEEL;1886/1925
> > ABBEEL;1887/603
> > ABBEEL;1887/1813
> > ABBEEL;1894/1444
> > ABBEEL;1898/3449
> > ABBEELS;1888/1401
> > ABBEELS;1889/562
> > ABBEELS;1891/2371
> > ABBEELS;1899/3088
> > ABBEELS;1902/732
> > ABBEELS;1903/1509
> > ABBEELS;1904/1317
>
> You can do it in two passes, one to prefix each item on its own line:
>
>   :%s/\%(^\([^;]*\).*\)\@<=\(;[^;]*\)/\r\1\2/g
>
> and then a second command to delete all the remnant lines (ones that
> are just the prefix which don't have a ";" in them):
>
>   :v/;/d
>
> -tim

Thanks a lot!

//meine

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20200717140522.GB999%40trackstand.