Wednesday, September 7, 2022

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

Thanks Tim and Arun,

If I use vim, how can I put the output after the data? as in:

----------
1001 John Sena 40000
1002 Jafar Iqbal   60000
1003 Meher Nigar   30000
1004 Jonny Liver   70000

xx1001yyy, xx1002yyy, xx1003yyy, xx1004yyy
----------

What I did was to
go to the data and do:
vipygPO<Esc>gv:'<,'>s/\(\S\+\)\>.*\n/xx\1yyy, / | s/, $//<Enter>

but, is there any other shorter way?
On Tuesday, September 6, 2022 at 6:47:05 PM UTC-5 Arun wrote:
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 <v...@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+u...@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/c96bcca3-245c-449c-b7a8-925d068e9d49n%40googlegroups.com.

No comments:

Post a Comment