简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM