This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Extraction of every n-th and m-th columns

Hi. Everyone!

I have a dataset similar to below. It has multiple samples, and each sample has multiple values.

ID    Sample1                     Sample2                    Sample3
xx    val1 val2 va3 val4 val5     val1 val2 va3 val4 val5    val1 val2 va3 val4 val5

How can I extract multiple columns from each sample (i.e. val2 and val4 from each sample) assuming all the data are tab delimited?

Thank you!

bash command

1 answer

$ awk -v M=1234 -v N=4567 -v FS="\t" -v OFS="\t" '{ print $M, $N; }' in.txt > out.txt

Replace M and N with your column indices of interest.

You could also use cut -fM,N in.txt > out.txt, so long as M < N. Using awk gets around quirks in cut.

Log in to answer this question.