How to have the sed output side by side?

我的梦境 提交于 2019-12-01 23:06:15

Using awk:

awk 'ORS="\t"' file

$ awk 'ORS="\t"' file
Valine, leucine and isoleucine degradation      Histidine metabolism    Ascorbate and aldarate metabolism       Lysine degradation      Glycerolipid metabolism 

If you wish to use sed then:

$ sed ':a;N;s/\n/\t/;ba' file
Valine, leucine and isoleucine degradation      Histidine metabolism    Ascorbate and aldarate metabolism       Lysine degradation      Glycerolipid metabolism

This is really what paste(1) is for:

$ paste -s "$file"
Valine, leucine and isoleucine degradation  Histidine metabolism    Ascorbate and aldarate metabolism   Lysine degradation  Glycerolipid metabolism

Here's what the manpage says the -s flag should do:

Concatenate all of the lines of each separate input file in command line order. The <newline> of every line except the last line in each input file shall be replaced with the <tab>, unless otherwise specified by the -d option.

You can also process standard input by using a - instead of the filename.

somecommand | paste -s -

What's the difference between tr '\n' '\t' and paste -s (with an implied tab delimiter)? The former will strip even the trailing newline, but paste will leave the final newline intact. Also, paste can handle both standard input and files, but tr can only handle standard input.

You could use tr:

tr '\n' '\t' < inputfile

For your input, it'd produce:

Valine, leucine and isoleucine degradation      Histidine metabolism    Ascorbate and aldarate metabolism       Lysine degradation      Glycerolipid metabolism

Using sed:

sed '$!{:a;N;s/\n/\t/;ta}' inputfile

You can use paste in serial mode:

paste -s file

You can use xargs like this:

$ xargs -n15 <file
Valine, leucine and isoleucine degradation Histidine metabolism Ascorbate and aldarate metabolism Lysine degradation Glycerolipid metabolism

Note 15 is the number of words in your file. You could write a bigger number like xargs -n50 < file to make sure everything printed in the same line.

Also printf '%s ' $(< file) or printf '%s ' $(cat file) if your shell doesn't have $(< ...).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!