{"id":7547,"date":"2014-10-12T21:19:05","date_gmt":"2014-10-13T02:19:05","guid":{"rendered":"https:\/\/gregladen.com\/blog\/?p=7547"},"modified":"2014-10-11T21:29:22","modified_gmt":"2014-10-12T02:29:22","slug":"one-liner-to-turn-a-column-of-text-into-a-comma-separated-list","status":"publish","type":"post","link":"https:\/\/gregladen.com\/blog\/2014\/10\/12\/one-liner-to-turn-a-column-of-text-into-a-comma-separated-list\/","title":{"rendered":"One liner to turn a column of text into a comma separated list"},"content":{"rendered":"<p>You have the following list:<\/p>\n<pre>Firstline\r\nSecondline\r\nThirdline\r\nFourthline\r\nFifthline\r\nSixthline<\/pre>\n<p>You want to turn it into something that looks like this:<\/p>\n<pre>\r\nFirstline,Secondline,Thirdline,Fourthline,Fifthline,Sixthline<\/pre>\n<p>All you have is a stick of bubble gum, a broken lightbulb, and a bash shell with the usual tools installed.  How would you do it?<\/p>\n<p>Here are a few suggestions.<\/p>\n<pre>\r\nperl -pi.bak -e 'unless(eof){s\/\\n\/,\/g}' textfile\r\n<\/pre>\n<p>This will produce a backup called textfile.bak and modify the original file.<\/p>\n<p>A rather involved sed one liner could do it:<\/p>\n<pre>\r\nsed -n 's\/.*\/&,\/;H;$x;$s\/,\\n\/,\/g;$s\/\\n\\(.*\\)\/\\1\/;$s\/\\(.*\\),\/\\1\/;$p'\r\n<\/pre>\n<p>Using tr:<\/p>\n<pre>\r\ncat textfile | tr '\\n' ','\r\n<\/pre>\n<p>There are numerous other possibilities. Free to make suggestions.  Also, how would you reverse the process? <\/p>\n","protected":false},"excerpt":{"rendered":"<p>You have the following list: Firstline Secondline Thirdline Fourthline Fifthline Sixthline You want to turn it into something that looks like this: Firstline,Secondline,Thirdline,Fourthline,Fifthline,Sixthline All you have is a stick of bubble gum, a broken lightbulb, and a bash shell with the usual tools installed. How would you do it? Here are a few suggestions. perl &hellip; <a href=\"https:\/\/gregladen.com\/blog\/2014\/10\/12\/one-liner-to-turn-a-column-of-text-into-a-comma-separated-list\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">One liner to turn a column of text into a comma separated list<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[164,687,57,688],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p5fhV1-1XJ","jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/gregladen.com\/blog\/wp-json\/wp\/v2\/posts\/7547"}],"collection":[{"href":"https:\/\/gregladen.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gregladen.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gregladen.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gregladen.com\/blog\/wp-json\/wp\/v2\/comments?post=7547"}],"version-history":[{"count":3,"href":"https:\/\/gregladen.com\/blog\/wp-json\/wp\/v2\/posts\/7547\/revisions"}],"predecessor-version":[{"id":7550,"href":"https:\/\/gregladen.com\/blog\/wp-json\/wp\/v2\/posts\/7547\/revisions\/7550"}],"wp:attachment":[{"href":"https:\/\/gregladen.com\/blog\/wp-json\/wp\/v2\/media?parent=7547"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gregladen.com\/blog\/wp-json\/wp\/v2\/categories?post=7547"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gregladen.com\/blog\/wp-json\/wp\/v2\/tags?post=7547"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}