【发布时间】:2015-05-27 20:38:25
【问题描述】:
我有这个文件
mean.cel sample.cel
1 2
3 4
我想插入第二列标题的部分名称的列
这将是预期的结果
mean.cel newcolumn sample.cel
1 sample 2
3 sample 4
我该怎么做?
【问题讨论】:
我有这个文件
mean.cel sample.cel
1 2
3 4
我想插入第二列标题的部分名称的列
这将是预期的结果
mean.cel newcolumn sample.cel
1 sample 2
3 sample 4
我该怎么做?
【问题讨论】:
我会说...
$ 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
为了使其更加健壮,允许您拥有多个字段(不仅仅是 2 个),我们可以说:
awk '{$1 = $1 OFS (NR==1?"new column":"sample")} 1' file
这里的想法是向第一个字段附加一个新值。通过使用NR,我们根据是在第一行还是在其他行来切换行为。
【讨论】:
{ $1 = $1 OFS "sample" } 1 使其适用于任意数量的列。
{$1 = $1 OFS (NR==1?"new column":"sample")} 1。感谢您的想法!
sed '1 {s/ / NewColumn /;b;}
s/ / Sample /' YourFile
【讨论】: