Wednesday, September 7, 2022

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

On 2022-09-07 08:14, andalou wrote:
> 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

With the vim suggestions I gave, item #1 was to make a duplicate
copy of the data and then modify that:

>> 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

like

:%t$

If so, you'd want to tweak the range to just operate on that data:

>> 2) over the range of that data, do a substitute
>> :%s/^\(\S\+\) .*/xx\1yyy,
>> to put the prefix/suffix/comma in place

making that something like

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

>> 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 ""}'

In this case, you'd accrue them all, (re)printing each row as you
process it, then print the joined results at the end

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

-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/Yxi3UpFLg%2B2tjL7c%40thechases.com.

No comments: