How to have the sed output side by side?

不羁岁月 提交于 2019-12-20 03:23:35

问题


I want to have standard output for KEGG pathways of a gene placed side by side no matter how many lines of the KEGG pathways it has. For example, a gene TT123456 is involves in several pathways:

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

By using the sed command

sed '$!N;s/\n/\t/'

I able to have two lines joined side by side

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

But, I would like to have the output as

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

I have been searching around, but, I failed to find a good solution.

Could the community please shares your expertise with me?

Thank you.


回答1:


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



回答2:


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.




回答3:


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



回答4:


You can use paste in serial mode:

paste -s file



回答5:


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.




回答6:


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



来源:https://stackoverflow.com/questions/22301682/how-to-have-the-sed-output-side-by-side

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