Making a new header depending on entries from old header
1
1
Entering edit mode
3.7 years ago
ellieuk ▴ 40

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 • 495 views
ADD COMMENT
4
Entering edit mode
3.7 years ago
Ram 43k

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)}'
ADD COMMENT

Login before adding your answer.

Traffic: 1984 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6