For a full list of BASHing data blog posts see the index page.
How to stack columns
A simple case. Here's "fileA", a tab-separated table (TSV) with some missing values:
aaa | ggg | mmm | sss | yyy |
bbb | hhh | nnn | ttt | zzz |
ccc | iii | ooo | uuu | |
ddd | jjj | vvv | ||
kkk | qqq | www | ||
fff | lll | rrr | xxx |
The easiest way I know to stack up the columns (column 1 on top of column 2 on top of column 3, etc) is to first get the number of columns from the first line (I use AWK for that job), then print the results of progressively cutting out each column:
cols=$(awk -F"\t" 'NR==1 {print NF}' fileA)
for ((i=1;i<="$cols";i++)); do cut -f"$i" fileA; done
Not so simple. Stacking gets a little trickier when adjoining columns need to be stacked together. For example, here's "fileB", again tab-separated, with each group of letters having a corresponding number:
aaa | 001 | ggg | 007 | mmm | 013 | sss | 019 | yyy | 025 |
bbb | 002 | hhh | 008 | nnn | 014 | ttt | 020 | zzz | 026 |
ccc | 003 | iii | 009 | ooo | 015 | uuu | 021 | ||
ddd | 004 | jjj | 010 | vvv | 022 | ||||
kkk | 011 | qqq | 017 | www | 023 | ||||
fff | 006 | lll | 012 | rrr | 018 | xxx | 024 |
A simple approach is to loop through the odd-numbered columns, cutting and stacking the odd-numbered columns together with the next even-numbered ones:
cols=$(awk -F"\t" 'NR==1 {print NF}' fileB)
for ((i=1;i<="$cols";i+=2)); \
do cut -f"$i","$(($i+1))" fileB; done
Last update: 2020-11-25
The blog posts on this website are licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License