leleonp
leleonp

Reputation: 67

Insert column with the same string in all lines

I have this file

mean.cel sample.cel
1  2
3  4

I would like to insert a column with a partial name of the header of the second column

This will be the expected result

mean.cel newcolumn sample.cel
1  sample 2
3  sample 4

How can i do this?

Upvotes: 1

Views: 1246

Answers (2)

fedorqui
fedorqui

Reputation: 289835

I would say...

$ awk 'NR==1{print $1, "new column", $2; next} {print $1, "sample", $2}' file
mean.cel new column sample.cel
1 sample 2
3 sample 4

To make it more robust, allowing you to have multiple fields (not just 2), we can say:

awk '{$1 = $1 OFS (NR==1?"new column":"sample")} 1' file

The idea here is to append a new value to the 1st field. By playing with NR we switch the behaviour depending on being in the 1st line or in the rest of them.

Upvotes: 3

NeronLeVelu
NeronLeVelu

Reputation: 10039

sed '1 {s/ / NewColumn /;b;}
s/ / Sample /' YourFile
  • based on your sample and using space as separator

Upvotes: 0

Related Questions