I want to transpose a 3-column text file to three separate files with one line each. In other words, take this file:
in_file
1 22 0.8
4 21 0.73
3 30 1.56
5 12 0.92
and produce the following 3 files:
out_file1
1 4 3 5
out_file2
22 21 30 12
out_file3
0.8 0.73 1.56 0.92
I tried to use cut to do this:
cut -d' ' -f1 in_file | tr -s '\n' '\t' >> out_file1
cut -d' ' -f2 in_file | tr -s '\n' '\t' >> out_file2
cut -d' ' -f3 in_file | tr -s '\n' '\t' >> out_file3
however, what I get is this:
out_file1
1 4 3 5
out_file2
22 21 30 12
out_file3
0.8
0.73
1.56
0.92
I can't figure out why it works for the first 2 columns and not the third.
Grateful for any help!
Your problem is most likely the delimiter which you've typed as a literal tab instead of \t
, here's a loop that does all three files:
for i in {1..3}; do
cut -d$'\t' "-f${i}" in_file | column >> "outfile_${i}.txt"
done
Here we loop over a sequense of {1..3}
, set the delimiter to tab with the special syntax: -d$'\t'
, pipe the data to column
which automatically lines up the values and append that to the correct file.