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.
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 $(< ...)
.
来源:https://stackoverflow.com/questions/22301682/how-to-have-the-sed-output-side-by-side