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

15 views
Skip to first unread message

andalou

unread,
Sep 6, 2022, 5:45:55 PM9/6/22
to vim_use

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

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

-- 
Cesar

Tim Chase

unread,
Sep 6, 2022, 6:43:59 PM9/6/22
to vim...@googlegroups.com
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





Arun

unread,
Sep 6, 2022, 7:47:05 PM9/6/22
to vim...@googlegroups.com
This seems to work well too (tested with 8.1):
  %s/\(\S\+\)\>.*\n/xx\1yyy,/

Regards,
-Arun

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

andalou

unread,
Sep 7, 2022, 11:14:52 AM9/7/22
to vim_use
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?

Tim Chase

unread,
Sep 7, 2022, 11:23:06 AM9/7/22
to vim...@googlegroups.com
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





Marc Chantreux

unread,
Sep 9, 2022, 4:26:03 PM9/9/22
to vim...@googlegroups.com
On Tue, Sep 06, 2022 at 10:43:50PM +0000, Tim Chase 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'll use

:%!sed 's/ .*/yyy,/'|tr '\n' ' '

regards
--
Marc Chantreux
Pôle de Calcul et Services Avancés à la Recherche (CESAR)
http://annuaire.unistra.fr/p/20200
Reply all
Reply to author
Forward
0 new messages