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

前端 未结 9 1977
囚心锁ツ
囚心锁ツ 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:07

    Simple way to join the lines with space in-place using ex (also ignoring blank lines), use:

    ex +%j -cwq foo.txt
    

    If you want to print the results to the standard output, try:

    ex +%j +%p -scq! foo.txt
    

    To join lines without spaces, use +%j! instead of +%j.

    To use different delimiter, it's a bit more tricky:

    ex +"g/^$/d" +"%s/\n/_/e" +%p -scq! foo.txt
    

    where g/^$/d (or v/\S/d) removes blank lines and s/\n/_/ is substitution which basically works the same as using sed, but for all lines (%). When parsing is done, print the buffer (%p). And finally -cq! executing vi q! command, which basically quits without saving (-s is to silence the output).

    Please note that ex is equivalent to vi -e.

    This method is quite portable as most of the Linux/Unix are shipped with ex/vi by default. And it's more compatible than using sed where in-place parameter (-i) is not standard extension and utility it-self is more stream oriented, therefore it's not so portable.

提交回复
热议问题