merit_2
merit_2

Reputation: 471

Regex list/column to comma delimited

I have a column of data, in this case captured from a website. I would like to convert this list into a comma separated list using regex either in the terminal or gedit, etc..

My list:

Liam
Noah
William
James
Oliver
Benjamin

What I want is:

Liam, Noah, William, James, Oliver, Benjamin

or

(Liam, Noah, William, James, Oliver, Benjamin)

or similar.

What I have tried is ^([A-Za-z]+)$("$1",) . I think it finds each name but it is not replacing anything.

It would also be great if something like this worked with numbers as well. Like,

10
20
30
pie

to

10,20,30,pie

Upvotes: 0

Views: 152

Answers (1)

Gilles Quénot
Gilles Quénot

Reputation: 184955

Like this:

perl -i -pe 's/\n/, /' file

Output:

Liam, Noah, William, James, Oliver, Benjamin,

Or better:

perl -0ne 'my @a = (split /\n/, $_); print join (", ", @a) . "\n"' file

Output:

Liam, Noah, William, James, Oliver, Benjamin

Upvotes: 1

Related Questions