Tuesday, September 6, 2022

Re: Print the first field of a table in one line separated by commas

This seems to work well too (tested with 8.1):
  %s/\(\S\+\)\>.*\n/xx\1yyy,/

Regards,
-Arun

On Tue, Sep 6, 2022 at 3:43 PM Tim Chase <vim@tim.thechases.com> wrote:
On 2022-09-06 14:45, andalou wrote:
> I have the table:
> 1001 John Sena 40000
> 1002 Jafar Iqbal   60000
> 1003 Meher Nigar   30000
> 1004 Jonny Liver   70000
> I'd like to print the first field, preceded by xx, and followed by yyy,
> separated by commas, as in:
> xx1001yyy, xx1002yyy, xx1003yyy, xx1004yyy

I'd do it in two (or three) passes:

1) optionally copy the data and paste it where you want the resuts
if you want to preserve it

2) over the range of that data, do a substitute

  :%s/^\(\S\+\) .*/xx\1yyy,

to put the prefix/suffix/comma in place

3) then join that range

  :%j

(and optionally remove the trailing comma)

> And I'd like to get this output after the table. I think I can do it
> with awk, but I'd like to do it with vim, if it is possible

If you want to do it with awk:

  awk '{printf("%sxx%syyy", NR==1?"":", ", $1)}END{print ""}'

-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 vim_use+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/YxfNJuDf//V5NXyp%40thechases.com.

--
--
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 vim_use+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/CAJUkyiHOwn%3DgN6rCZ9fdZaXrLaw7bBgNTt6UOg%3DRBWF5AnbuTw%40mail.gmail.com.

No comments:

Post a Comment