This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Making a new header depending on entries from old header

For many this will be very basic, but for me it's driving me nuts!

I have a tab-delimited header: sampleA sampleB sampleC

Depending on the number of samples I have, I want to make a new header as such:

For 3 samples: sampleA sampleB sampleC output001 output002 output003

For 4 samples: sampleA sampleB sampleC sampleD output001 output002 output003 output004 and so on...

I can duplicate the initial header to get the correct number of columns: paste header header

However, I want new column names.. and that's where I'm stuck....

unix

1 answer

Use awk and NF. You can use printf to pad 0s.

Example:

echo -e "sampleA\tsampleB\tsampleC" | awk -F"\t" -vOFS="\t" '{printf($0"\t"); for(i=1;i<NF;i++) printf("output%03d\t",i); printf("output%03d",i)}'

You hero! That's amazing.

Glad it helped. If it resolved your question, please mark it as accepted.

Upvote|Bookmark|Accept

Log in to answer this question.