Concise and portable “join” on the Unix command-line

前端 未结 9 1937
囚心锁ツ
囚心锁ツ 2020-11-29 00:53

How can I join multiple lines into one line, with a separator where the new-line characters were, and avoiding a trailing separator and, optionally, ignoring empty lines?

9条回答
  •  一向
    一向 (楼主)
    2020-11-29 01:15

    Perhaps a little surprisingly, paste is a good way to do this:

    paste -s -d","
    

    This won't deal with the empty lines you mentioned. For that, pipe your text through grep, first:

    grep -v '^$' | paste -s -d"," -
    

提交回复
热议问题