How to have the sed output side by side?

后端 未结 6 1895
忘了有多久
忘了有多久 2021-01-22 10:55

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

相关标签:
6条回答
  • 2021-01-22 11:07

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

    0 讨论(0)
  • 2021-01-22 11:08

    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
    
    0 讨论(0)
  • 2021-01-22 11:15

    You can use paste in serial mode:

    paste -s file
    
    0 讨论(0)
  • 2021-01-22 11:19

    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.

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2021-01-22 11:30

    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.

    0 讨论(0)
提交回复
热议问题