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
Also printf '%s ' $(< file)
or printf '%s ' $(cat file)
if your shell doesn't have $(< ...)
.
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
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.
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
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.