Reputation: 471
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
Reputation: 184955
Like this:
perl -i -pe 's/\n/, /' file
Liam, Noah, William, James, Oliver, Benjamin,
Or better:
perl -0ne 'my @a = (split /\n/, $_); print join (", ", @a) . "\n"' file
Liam, Noah, William, James, Oliver, Benjamin
Upvotes: 1