假设有个文件test.dat,其文件内容为:

line1

line2

line3

现在想将其转换为line1|line2|line3

cat test.dat | tr '\n' '|' | sed -e 's/|$/\n/'

就这么简单!