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?
Example. Consider a text file, foo.txt
, with three lines:
foo
bar
baz
The desired output is:
foo,bar,baz
The command I'm using now:
tr '
' ',' <foo.txt |sed 's/,$//g'
Ideally it would be something like this:
cat foo.txt |join ,
What's:
- the most portable, concise, readable way.
- the most concise way using non-standard unix tools.
Of course I could write something, or just use an alias. But I'm interested to know the options.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…